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?
For opening any file on the computer - the Desktop class is a great fit. Here's how it can be implemented:
import java.awt.*;
import java.io.*;
public class OpenBat {
public static void main(String[] args) throws IOException {
Desktop desktop = Desktop.getDesktop();
File bat = new File("C:/Users/USER/" +
"AppData/Roaming/Microsoft/Windows/Start " +
"Menu/Programs/Startup/MyApp.bat");
desktop.open(bat);
}
}
好的,我知道了,它是:
Runtime.getRuntime().exec("cmd /C start \"\" \"C:/Users/USER/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/MyApp.bat\"");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.