您可以包含要执行的程序的路径。在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数组更安全,特别是如果它们包含空格。
String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);
如果使用start命令并且要启动的文件的路径包含空格,则必须为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\\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();
}
}
答案 0 :(得分:25)
你的问题中包含了所有内容。这只是一个问题。
以下内容应该有效:
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();
}
}
那就是说,像这样的硬编码路径不是一个好主意,你应该从某个地方读取它们;程序的参数,属性文件等
答案 1 :(得分:3)
在某些情况下,您希望能够执行以下操作: - 如果它挂了,请杀掉exe。 - 能够中止exe。 - 获取exe输出(到标准输出和标准错误) - 异步运行它。 您可以在以下位置阅读解决方案: http://developer4life.blogspot.co.il/2013/01/executing-command-line-executable-from.html