问题
Actually I am working Wolfram Mathematica,we don't have any functions for running external programs, so I want to do this App using Java technology.
In my organization, C developing team, developed one Application and given to us in a .exe format.Know I want to run that .exe file from Java.
I have a .exe name as ThMapInfratab1-2.exe under C:/Users/Infratab Bangalore/Desktop/Rod's directory.
once we run the .exe file, automatically I takes .txt file as a input name as TherInput.txt from the same directory (C:/Users/Infratab Bangalore/Desktop/Rod's)
For my conformation, I run the ThMapInfratab1-2.exe file manually using command prompt in the following way.it's working great.
C:\Users\Infratab Bangalore\Desktop\Rod's>ThMapInfratab1-2.exe
Finally .exe file exports t .txt files as a output into same directory(C:/Users/Infratab Bangalore/Desktop/Rod's).
These same thing, I want to do using Java.can you explain with my directories.
For this, I wrote the following code but it's not working.
import java.io.IOException;
public class ProcessBuilderSample {
public static void main(String args[]) throws IOException {
Process process = new ProcessBuilder(
"C:\\Users\\Infratab bangalore\\Desktop\\Rod's\\ThMapInfratab1-2.exe")
.start();
}
}
If anyone knows,suggest me.
Thanks.
.
回答1:
You need to execute exec() method of Runtime that returns Process instance or use ProcessBuilder class methods.
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("C:\\Users\\Infratab Bangalore\\Desktop\\Rod's\\ThMapInfratab1-2.exe");
You can use ProcessBuilder as
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
回答2:
You can do it like this:
Process process=Runtime.getRuntime().exec("C:\\Users\\Infratab Bangalore\\Desktop\\Rod's\\ThMapInfratab1-2.exe");
回答3:
Process process=Runtime.getRuntime().exec("C:\Users\Infratab Bangalore\Desktop\Rod's>ThMapInfratab1-2.exe");
来源:https://stackoverflow.com/questions/17826732/how-to-run-exe-file