简体   繁体   English

如何在JAVA中执行exe命令?

[英]How do I execute an exe command in JAVA?

I have an exe file which takes a file name as input. 我有一个exe文件,它以文件名作为输入。 When I execute it as a command like: 当我将其作为命令执行时:

xyz.exe c:\\input.txt c:\\ouput.txt

It all works as expected. 一切都按预期工作。

But how to execute this is java? 但是如何执行这是java?

This is the one i used and am not getting the ouput in the files: 这是我使用的文件,没有在文件中输出:

String[] str = {"c:/input.txt","c:/output.txt"};
Process p = rt.exec("c:/xyz.exe",str); 

You're using the method: 您正在使用方法:

public Process exec(String command,
                    String[] envp)

where envp is a ( quote ) "array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process." 其中envp是一个( 引号“字符串数组,其每个元素均具有以name = value格式表示的环境变量设置,如果子进程应该继承当前进程的环境,则为null。

Try this instead: 尝试以下方法:

String[] command = {"c:/xyz.exe", "c:/input.txt", "c:/output.txt"}; 
Process p = Runtime.getRuntime().exec(command);
// ...

Also read this article that explains the pitfalls of Runtime.exec(...) : http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html 另请阅读这篇文章,它解释了Runtime.exec(...)的陷阱: http : //www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

使用Runtime.exec或Processbuilder API

我相信这应该可以回答您的问题http://www.daniweb.com/software-development/java/threads/133710

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM