How can I run a Java from windows batch file but hiding the command window?

18,453

Solution 1

Use javaw.exe rather than java.exe this should drop the shell

Solution 2

you can hide it like this:

start javaw -jar example.jar

Or if you call it from inside of another project you can call like like this:

try {
        Process p = Runtime.getRuntime().exec("cmd /c start /B example.bat");
        p.waitFor();
    } catch (InterruptedException ex) {
        Logger.getLogger(OpenFileFromCmd.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(OpenFileFromCmd.class.getName()).log(Level.SEVERE, null, ex);
    }

If you have any question just ask me :))

Solution 3

Start your bat file using the windows "start" command

Start "" /B "c:\myFolder\MyJavaLauncher.bat"

The "/B" option tell to start the program without showing the typical command window.

Solution 4

Runtime.getRuntime().exec("cmd /c start /B example.bat");

Solution 5

try {
        String[] command = {"cmd.exe", "/C", "Start /B" , "C:\\path\\urbatname.bat"};
        Process p =  Runtime.getRuntime().exec(command);           
    } catch (Exception ex) {
        ex.printStackTrace();
    }

The /B allow you to execute the batch file invisibly. Just have the codes above in your function and it will work.

Share:
18,453
hamed
Author by

hamed

Updated on July 16, 2022

Comments

  • hamed
    hamed almost 2 years

    I have a .bat file include this command java -jar example.jar that runs example.jar.

    I don't want cmd.exe to be visible on screen when the file is being executed. Is this possible?

  • hamed
    hamed over 12 years
    thank you guy,please help me about 'javaw.exe' whit an example.
  • Chris Forrence
    Chris Forrence over 10 years
    Hello, and welcome to Stack Overflow! As it stands, your answer seems little...sparse. Could you please edit your answer and add an explanation as to why to run this particular command?