Invoke jar file via bat and get the output to same console - java

I have test.jar which just contain a single class and it has a main method. That just prints out some string to std out. I need to run this jar using .bat file. So I used
#echo off
start java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
in my script.
When I run the bat file the output does not print to the same console. Instead it is opened in another window.
( What I understand is I should put some 'echo variable' where the output of the java program should be assigned to 'variable'. Or There might be some other ways ).
What should I do?
Thank You !

The start is what is causing the new window to pop up and the output is going there. Change your bat file to say this instead:
#echo off
java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
You can also remove the #echo off if you want. If you remove that line, the windows shell will print out the lines it's executing in your bat file. I'd personally leave #echo off there except when you're trying to debug the bat file.

Related

How to run batch file with parameters in java?

I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -
#echo off
set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*
#echo on
It is running successfully from command prompt with parameters.
Now I would like to run it from another JAVA program.
Please suggest.
Thanks!
I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.
Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");

creating a bat file which sends to background and closes (1 file)

im trying to do something simple, i want to start Burp Suite with extra java memory, but i don't want the CMD window to stay open.
If i don't use a .bat file and simple open cmd and type start /b "" java -jar -Xmx2g burpsuite_pro_v1.6.07.jar, Burp opens, the the process is sent to background, but the CMD window stays open. i can, however, close it manually and Burp will keep working.
when i try to put the thing into a CMD window, it will not even be sent to background, Burp stays dependent on the CMD, and i cant even add exit to the file.
i tried to solve the issue by following:
run bat file in background - this worked, but required me to have THREE files, i prefer a more elegant "1 file solution"
Just add the below line in your bat file and the java procees will run in background with no window open!
start javaw {Path of Your jar or Java file}
The following batch file commands should accomplish your purpose:
start "" /B java.exe -Xmx2g -jar burpsuite_pro_v1.6.07.jar
exit
You can probably even leave out the /B switch.
References
How can I run a program from a batch file without leaving the console open after the program start?
How to use the start command in a batch file?
Turns out the solution was simple: using javaw.
the issue was with using java.exe, some attempts even closed the CMD but opened a java.exe window (blank)
modding the file to contain:
start javaw -Xmx2g -jar burpsuite_pro_v1.6.07.jar solved it for me.

System.out.println in jar

I have this java program that I want to package within a jar.
It compiles fine and it compiles without errors into a jar. But when I double click it, it wont start. I know most jar applications uses JFrame and that works fine. But is it possible to make it command prompt/shell based? (Like Displaying System.out.println)
Example is it possible to execute this code by double clicking a jar:
public class Hello
{
public static void main( String[] args )
{
System.out.println( "Hello, World!" );
}
}
There is no problem doing like that. But where do you expect to see the output?
If you execute from the console as java -jar my jar.jar you will see your "Hello, World".
If you want to double-click you'll need to create a JFrame.
You can change the file association for jar files to have them open a console.
(This is for Windows)
Open Command Processor
Type ftype jarfile
You will get something like:
jarfile="C:\Path\To\Java\bin\javaw.exe" -jar "%1 %*"
Enter ftype jarfile "C:\Path\To\Java\bin\java.exe" -jar "%1" %* (You may need administrator privileges to do this).
I don't see anyone mentioning the obvious solution: make a .cmd (or .bat) file containing the command everyone is talking about -- java -jar YourJar.jar. You can double-click on the .cmd file and a console window will open. It will also close imediately as your program exits, so the program should wait for a keypress before exiting.
You can make a jar an executable by including a manifest file which contains the name of the class that has your main method (in your example that would be Hello). Here is a link that details what you need to do.
Sure. First try to run your program as following:
java -cp yourjar.jar Main
where Main is a fully qualified class name of your main class.
When this is working fine and if you creted exacutable jar try to run your program as following:
java -jar yourjar.jar
When this is working double click should work too unless you mapped extension jar to program other than javaw that is done by default when you are installing JRE.
If you want to display the "Hello, world!" String you have to execute your code from the command line:
jar -jar your_jar_name.jar
The output gets forwarded to the console but without eclipse, there isn't any! The only way to work around this is to launch the program in command prompt and then you will get the output from the program.

Executable Jar not Displaying Output

I have an executable jar that I know is executing, because I put two distinct beeps at the beginning of the code that I hear whenever I double click on it or when I run it through the cli. Even when I run it through the command line however, it does not display output nor prompt for input when I use System.out/System.in respectively. Everything is functional when I run it through eclipse.
How do I get the .jar to output/input to the same command line I executed it in?
Assuming java is in your PATH, you should lauch your program as follows :
java -jar myProgram.jar
If you want to run it without opening the command line, you could create a .cmd file in the same directory with this content :
#echo off
java -jar myProgram.jar
pause
then, double-click on the .cmd file.

Starting a blank console

IS there any way to start a blank console in Windows platform ?
I'm writing a CLI where i want to open a separate window where user can log in and write their own command. When executing with cmd /c start command, it starts windows standard console.
Is there any other command ???
Assuming you're trying to start a java jar file, use a command like this:
start /d "%~dp0" java -jar "%~dp0\fpa.jar"
%~dp0 expands to the drive letter and path in which the batch file is located. Use that if you want to want to make sure that the PWD when running is the same location as the batch file. Otherwise, juse use
start java -jar "%~dp0\fpa.jar"
This will make sure that the batch file works even if you run it when not in the same directory as the jar file, as long as the jar file is in the same directory as the batch file.
You may need to make sure that java is in your path by having a line like
set path=jre6\bin;%PATH%
Also, you can eliminate the command line windows that comes up (for a GUI program by example) by using javaw instead of java.
You can use batch file in windows which would in turn start application that would accept user input. Something similar to this :
start cmd /c myapp.bat

Categories

Resources