views:

1499

answers:

6

You launch a java program from a console (maybe using a .bat script). I don't want the console to remain visible, I want to hide it.

Is there a simple way to do this ? Without JNI ?

Thanks.

+12  A: 

Use javaw.

http://java.sun.com/javase/6/docs/tooldocs/windows/java.html

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

johnstok
I took the liberty to update the link to a version that hasn't been eol-ed for quite some time.
Joachim Sauer
Calling javaw -jar allows you to close the console window without stopping execution of the jar you are running. The problem is that the window is still left open if you call javaw from the bat file. In the batch file, add start before the javaw command: start javaw -jar jarName.jar
IcyBlueRose
+3  A: 

You can hide the console by using javaw.exe (java without) instead of using java.exe.

One of the most useful associations to set up is to make *.jar files executable with java.exe. Then you can just type the name of the jar on the command line to start it executing.

If you use javaw.exe rather than java.exe you won’t see the console output. Watch out, Java installers often associate *.jar files with javaw.exe instead of java.exe, overriding your setting.

VonC
+1  A: 

This .bat trick works for general programs so I think it should also work for launching java program:

Call start program instead of just program in your .bat script

Lukman
+1  A: 

Thanks : writing start javaw ...

in the batch file instead of java ...

worked just fine !

Eric Nicolas
If you so wish you can officially accept an answer (by clicking the tick next to an answer) so that others with the same issue can find it easily...
johnstok
A: 

In case fo running from but file your script should look like start javaw start javaw -jar ***.jar

t3hnar
A: 

u can start the java with "start javaw". It will hide black console window.

Manikandan