Frank Frank - 2 years ago 90
Java Question

How to run a bat file in Startup directory with Java?

I tried both of the following:

Runtime.getRuntime().exec("cmd.exe /c C:/Users/USER/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/MyApp.bat");

Runtime.getRuntime().exec("cmd.exe /c \"C:/Users/USER/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/MyApp.bat\"");

Neither of them worked, the first one didn't have any error message, the second one had the following error message: Cannot run program "cmd.exe /c C:/Users/USER/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/MyApp.bat": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(

is in the Startup directory and I can run it by hand.

What's the correct way to run it from my Java app?

Answer Source

OK I figured it out, it is :

Runtime.getRuntime().exec("cmd /C start \"\" \"C:/Users/USER/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/MyApp.bat\"");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download