How to run .exe file? [duplicate]

时光毁灭记忆、已成空白 提交于 2020-01-16 04:30:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!