如何在java中执行命令行.exe文件 [英] how to execute command line .exe file in java
问题描述
- 我想使用java程序将avi文件转换为3gp
。 - 为此,我使用EM总计
视频转换器命令行2.43
,命令为
C:\EM
TVCC> TVCC -f E:\TestVideo\01。 avi -o
E:\OutputFiles\target.3gp - 我有一个程序来执行命令
在线exe文件< a href =http://www.rgagnon.com/javadetails/java-0014.html =nofollow noreferrer> http://www.rgagnon.com/javadetails/java-0014.html 这是:
其中包含空格的路径 $ b 您可以包括要执行的程序的路径。在Win plateform上,如果路径包含空格,则需要将引号括起来。
public class Test {
public static void main(String [] args)throws Exception {
Process p = Runtime.getRuntime()。exec(
\c:/ program files / windows / notepad.exe\);
p.waitFor();
}
}
如果您需要传递参数,字符串数组,特别是如果它们包含空格。
String [] cmd = {myProgram.exe,-o =这是一个选项};
Runtime.getRuntime()。exec(cmd);
如果使用start命令,要启动的文件的路径包含空格,开始命令的标题。
String fileName =c:\\Applications\\My Documents \\test.doc;
String [] commands = {cmd,/ c,start,\DummyTitle\,fileName};
Runtime.getRuntime()。exec(commands);
***任何人都可以帮我把上面的命令放在这段代码中? 。
这是我使用的确切的java代码:
public class Test {
public static void main(String [] args)throws Exception {
String [] cmd = {C:\\Program Files \\EM TVCC\\TVCC.exe,-f C:\\Program Files\\EM TVCC\\01.avi ,-o C:\\Program Files\\EM TVCC\\target.3gp};
Process p = Runtime.getRuntime()。exec(cmd);
p.waitFor();
}
}
解决方案 你的问题中有所有的部分。
如下所示应该可以工作:
public class Test {
public static void main(String [] args)throws Exception {
String [] cmd = {C:\\EM TVCC\\TVCC.exe,-f E:\\TestVideo\\01.avi,-o E:\\OutputFiles\\ target.3gp};
Process p = Runtime.getRuntime()。exec(cmd);
p.waitFor();
}
}
也就是说,一个好主意,你应该从某个地方读他们;您的程序的参数,属性文件等。
- 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.html which is:
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.
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[] 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.
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:
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();
}
}
解决方案 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.
这篇关于如何在java中执行命令行.exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
public class Test {
public static void main(String [] args)throws Exception {
Process p = Runtime.getRuntime()。exec(
\c:/ program files / windows / notepad.exe\);
p.waitFor();
}
}
如果您需要传递参数,字符串数组,特别是如果它们包含空格。
String [] cmd = {myProgram.exe,-o =这是一个选项};
Runtime.getRuntime()。exec(cmd);
String fileName =c:\\Applications\\My Documents \\test.doc;
String [] commands = {cmd,/ c,start,\DummyTitle\,fileName};
Runtime.getRuntime()。exec(commands);
public static void main(String [] args)throws Exception {
String [] cmd = {C:\\Program Files \\EM TVCC\\TVCC.exe,-f C:\\Program Files\\EM TVCC\\01.avi ,-o C:\\Program Files\\EM TVCC\\target.3gp};
Process p = Runtime.getRuntime()。exec(cmd);
p.waitFor();
}
}
public class Test {
public static void main(String [] args)throws Exception {
String [] cmd = {C:\\EM TVCC\\TVCC.exe,-f E:\\TestVideo\\01.avi,-o E:\\OutputFiles\\ target.3gp};
Process p = Runtime.getRuntime()。exec(cmd);
p.waitFor();
}
}
"C:\E.M. TVCC>TVCC -f E:\TestVideo\01.avi -o E:\OutputFiles\target.3gp"
public class Test {
public static void main(String[] args) throws Exception {
Process p = Runtime.getRuntime().exec(
"\"c:/program files/windows/notepad.exe\"");
p.waitFor();
}
}
String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);
String fileName = "c:\\Applications\\My Documents\\test.doc";
String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
Runtime.getRuntime().exec(commands);
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();
}
}
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();
}
}