问题
I have to open a .exe file from my Java program. So I tried following code First.
Process process = runtime.exec(\"c:\\\\program files\\\\test\\\\test.exe\");
But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors. So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file.
Following is my code:
BufferedWriter fileOut;
String itsFileLocation = \"c:\\\\program files\\\\test\\\\\"
System.out.println(itsFileLocation);
try {
fileOut = new BufferedWriter(new FileWriter(\"C:\\\\test.bat\"));
fileOut.write(\"cd\\\\\"+\"\\n\");
fileOut.write(\"cd \"+ itsFileLocation +\"\\n\");
fileOut.write(\"test.exe\"+\"\\n\");
fileOut.write(\"exit\"+\"\\n\");
fileOut.close(); // Close the output stream after all output is done.
} catch (IOException e1) {
e1.printStackTrace();
} // Create the Buffered Writer object to write to a file called filename.txt
Runtime runtime = Runtime.getRuntime();
try {
Process process =runtime.exec(\"cmd /c start C:\\\\test.bat\");
} catch (IOException e) {
e.printStackTrace();
}
The above code works perfectly. However, the command prompt is also opened at the back of my .exe (Application). It closes only after the .exe file exits..
I need to clse my command prompt when my application stats.
My .bat file will be like following after it is written by the program.
cd\\
cd C:\\Program Files\\test\\
test.exe
exit
回答1:
You don't need a console. You can execute a process using a working directory:
exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.
- command is the location of the .exe
- envp can be null
- dir, is the directory of your .exe
With respect to your code it should be...
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
回答2:
You can use Runtime.exec(java.lang.String, java.lang.String[], java.io.File) where you can set the working directory.
Or else you can use ProcessBuilder as follows:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
pb.directory(new File("myDir"));
Process p = pb.start();
回答3:
Another way of running a file is the following:
import java.awt.Desktop;
import java.io.File;
public static void open(String targetFilePath) throws IOException
{
Desktop desktop = Desktop.getDesktop();
desktop.open(new File(targetFilePath));
}
回答4:
The the Standard Code for Running bat or any other through command line using java is :
runtimeProcess = Runtime.getRuntime().exec("cmd /c start cmd.exe /C\""+backup_path+"\"");
int processComplete = runtimeProcess.waitFor();
and you can go on continue for multiple files using & supperator like : &&
回答5:
This would also work.
Process process = new ProcessBuilder("C:\\Users\\test\\Downloads\\Termius.exe").start();
It would start the .exe in that file location.
回答6:
Best way to run exe file
make java.awt.Desktop object and equal Desktop.getDesktop();
Desktop desktop = Desktop.getDesktop();
desktop.open("file path");
run exe file:
desktop.open("C:\\Windows\\System32\\cmd.exe");
or
desktop.open("C:/Windows/System32/cmd.exe");
run url :
desktop.browse(new URI("http://www.google.com"));
来源:https://stackoverflow.com/questions/10685893/run-exe-file-in-java-from-file-location