18

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 ?

Thomas Shields
  • 8,874
  • 5
  • 42
  • 77
Eric Nicolas
  • 1,507
  • 3
  • 17
  • 24

7 Answers7

34

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
johnstok
  • 96,212
  • 12
  • 54
  • 76
  • I took the liberty to update the link to a version that hasn't been eol-ed for quite some time. – Joachim Sauer May 25 '10 at 13:50
  • 6
    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 Sep 29 '10 at 15:59
  • 1
    Javaw.exe is the best way to do it! Attempting to manipulate the parameters of the CreateProcess won't work because java.exe creates the console, not CreateProcess. – dmihailescu Nov 14 '18 at 20:41
8

You can start a java application with start javaw. It will hide the black console window.

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
Manikandan
  • 81
  • 1
  • 1
5

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
  • 18,462
  • 6
  • 56
  • 66
4

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,262,500
  • 529
  • 4,410
  • 5,250
2

download jsmooth and create your own custom exe in a minute or two. Then just use that exe to launch your java app. You can even get slick and bundle a JRE with your app.

http://jsmooth.sourceforge.net

1-14x0r
  • 1,697
  • 1
  • 16
  • 19
0

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

t3hnar
  • 61
  • 2
0

Note, that you may need running javaw.exe by providing full path to the file, that may need adding quotes " in case there are spaces in the path. The quotes will trigger recognition of them as "title"-argument for the "start" command.

So, use following correct format:

start "MyTitle" "c:\Program Files (x86)\Java\jdk1.8.0_202\bin\javaw.exe" -jar myApp.jar

where title can be empty if needed

exadmin
  • 39
  • 1