closing a command line window in Java - java

I have created a small Java application in which there is some code that executes a batch file. The execution of the batch file leads to the command line window to be opened and to display some output messages. I would like to know if there is some way in Java to call this command line window to be closed from within the program...Thanks!

the command window should close automatically when the batch file completes.
and to run a batch file in background/invisibly, check other questions

Start java by using javaw or javaw.exe.
java (java.exe) runs with an associated console window,
javaw (javaw.exe) is the same but without the console window.
see the documentation for the java command.
On Windows also use start to invoke another shell
start javaw ClassName
I'm not sure for Linux&Co. Try using an & after the command to run it in the background
javaw ClassName &
The other way, closing the window from a batch started by Java:
I don't believe that is possible directly from within Java. You can only close the batch file by itself.
Is hard to help without knowing what that batch file is doing. You may try using the start command on windows or the & in Linux to run the process in the background (start has an option to open the window minimized [/MIN] or in the background [/B]).
Or try some hack like using AutoHotKey or some system functionality (some WinAPI-DLL or equivalent in other systems).

As an addition to NimChimpsky's answer:
If you run a batch file in Windows, Windows will automatically open a command window for the batch file, in case the batch file wants to print output or prompt for input. This also applies if you launch the bat file from a Java process.
Unfortunately, Windows itself apparently provides no way to launch a batch file without such a window. To avoid the window, you will have to run the batch file via some helper program. There are several available; google for "run bat no window" to find some.
If you just want the window to go away after the batch file terminates: That should happen automatically. If it does not, some program launched by the batch file is still running.

start /b [bat file name]

Related

Execute java application with one icon

I'm trying to replicate the behavior of Linux where we create an application launcher using Alacarte providing it the command and file name and Icon using batch
However I have never used batch.
The batch file is in the same directory as the java application.
The Batch contains the command as:
java -javaagent:app1.jar -jar app2.jar
Which does the job but It keeps CMD running in the background which during work I always accidentally close it which turns out closing the java app.
How can I can make it so it will disappear after launching the app and keep the app running
On Windows use javaw, as this will not open the console window. See also java vs javaw

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.

How to execute java application (.exe / .jar / .bat) without displaying popup console at all?

I want to execute java application without displaying popup console. It because in 64bit applicaton, the GUI (console) will created in different session ID. I even use Java Service Wrapper, but the batch file still showing an popup console. So, how can I execute java application without displaying popup console? Or there any alternative way?
Running following command in command prompt, or through a batch file should help you:
start javaw -jar -Xms1024m -Xmx1024m <Your Jar File>
Refer to this thread for more.

Java - System.out.println() viewable from application?

I have a Java application that runs great :) While uploading files, it uses the standard output to show progress : "System.out.println(...);".
When I run it in Eclipse, well it works perfectly, but when I run the JAR file, I don't see any console/terminal showing up and printing what I print through "System.out.println(...),".
How can I open a new terminal when my application is launched (it is a Swing application)?
Basically I want to be able to run the Swing application and show information on the side in a terminal / console. Why? Don't worry about why I want to do this ;)
Thanks a lot!
Regards.
Open terminal and run application as java -cp yourjar.jar YouMain or java -jar yourjar.jar if you jar is runnable.
I believe that you do not see output because you are running your application using javaw - the special windows-only variation of JVM that does not have STDOUT at all. If you want to click your application and see output map *.jar file to be opened using java instead of javaw. Alternatively write bat file that runs your application. In this case you will see console.
Use java instead of javaw to launch your application. Double-clicking on a jar executes it with javaw. Instead, open a command line window and type
java -jar thePathOfTheJarFile.jar
If you want to have something double-clickable, then write a shell script containing this command, and double-click the shell script instead of the jar.

How to call for the system console in Java?

I made a console-based Java application but every time I try to start the .jar file by clicking on it the program seems to be running but there is no console displayed. Is there specific code I must write in order to call for the system console?
Can you start the console first , change directory to where your jar file is and then run java -jar yiurjarfilename ?
The OS takes care of displaying console output. There is no code that you can write within Java to display or hide the "console" (because within Java, there's only standard output & error streams that you write to).
Windows usually leaves the console open after your program exits, but there might be a setting within the Java Runtime Environment that configures that behavior.
gcivil is right, you can see the results in console only if you start from console, if you are in windows you can open the command line Super + R and type cmd, then press enter (Super is the one with the Windows icon)
there you can type : java -jar "absolute path to your file" (don't forget the quotes)
another way is create a .bat file next to the .jar one, the bat file should contain
java -jar filename.jar
you don't need the quotes nor absolute path since it is next to the .bat file now you can double click that instead of the .jar
Once the app is terminated it will close the console, if you need to see what is next you have to add pause
java -jar filename.jar
pause

Categories

Resources