如何在java中执行命令行.exe文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2243993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to execute command line .exe file in java
提问by Ishan
- i want to convert an avi file to 3gp using java program.
- For this i am using "E.M. Total
Video Converter Command Line 2.43"
and the command for it is
"C:\E.M. TVCC>TVCC -f E:\TestVideo\01.avi -o E:\OutputFiles\target.3gp" - I got a program to execute command line exe file on site http://www.rgagnon.com/javadetails/java-0014.htmlwhich is:
- 我想使用 java 程序将 avi 文件转换为 3gp。
- 为此,我使用“EM Total Video Converter Command Line 2.43”,它的命令是
“C:\EM TVCC>TVCC -f E:\TestVideo\01.avi -o E:\OutputFiles\target.3gp” - 我有一个程序可以在网站http://www.rgagnon.com/javadetails/java-0014.html上执行命令行 exe 文件,它是:
Path to executable with spaces in them
带有空格的可执行文件的路径
You can include a path for the program to be executed. On the Win plateform, you need to put the path in quotes if the path contains spaces.
您可以包含要执行的程序的路径。在 Win 平台上,如果路径包含空格,则需要将路径放在引号中。
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
If you need to pass arguments, it's safer to a String array especially if they contain spaces.
如果需要传递参数,使用 String 数组更安全,尤其是当它们包含空格时。
String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);
If using the start command and the path of the file to be started contains a space then you must specified a title to the start command.
如果使用 start 命令并且要启动的文件路径包含空格,则必须为 start 命令指定标题。
String fileName = "c:\Applications\My Documents\test.doc";
String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
Runtime.getRuntime().exec(commands);
***Can anyone help me to put the above command in this code?***I dont know the syntax rules to put that command in the above code.Please help me.
***谁能帮我把上面的命令放在这段代码中?***我不知道把那个命令放在上面的代码中的语法规则。请帮帮我。
This is the exact java code i am using:
这是我正在使用的确切 Java 代码:
public class Test {
public static void main(String[] args) throws Exception {
String[] cmd = { "C:\Program Files\E.M. TVCC\TVCC.exe", "-f C:\Program Files\E.M. TVCC\01.avi", "-o C:\Program Files\E.M. TVCC\target.3gp" };
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
}
采纳答案by Glen
You've got all the pieces in your question. It's just a matter of putting it all together.
你已经得到了你的问题的所有部分。这只是将它们放在一起的问题。
Something such as the following should work:
诸如以下内容应该可以工作:
public class Test {
public static void main(String[] args) throws Exception {
String[] cmd = { "C:\E.M. TVCC\TVCC.exe", "-f E:\TestVideo\01.avi", "-o E:\OutputFiles\target.3gp" };
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
}
That said, hard coding paths like this isn't a good idea, you should read them from somewhere; arguments to your program, a properties file, etc.
也就是说,像这样的硬编码路径不是一个好主意,您应该从某个地方读取它们;程序的参数、属性文件等。
回答by user1151546
In some cases you want to be able to do more like: - Kill the exe in case it hung. - Be able to abort the exe. - Get the exe output (to the standard output and the standard error) - Run it asynchronously. You can read on a solution at: http://developer4life.blogspot.co.il/2013/01/executing-command-line-executable-from.html
在某些情况下,您希望能够做更多的事情: - 杀死 exe 以防它挂起。- 能够中止 exe。- 获取 exe 输出(到标准输出和标准错误) - 异步运行。您可以在以下位置阅读解决方案:http: //developer4life.blogspot.co.il/2013/01/executing-command-line-executable-from.html