Frank Frank - 7 months ago 11
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:


java.io.IOException: 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(ProcessBuilder.java:1048)


MyApp.bat
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

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\"");
Comments