我尝试使用 java 代码运行 Windows 命令,但代码无法正常工作并给出异常错误。 下面是代码
import java.io.*;
public class run_command
{
public static void main(String args[])
{
try
{
String command = "start firefox";
Process process = Runtime.getRuntime().exec(command);
}
catch(IOException e){ System.out.println(e); }
}
}
下面是异常错误
java.io.IOException: Cannot run program "start firefox": Create Process error=2, The system cannot find the file specified.
每个 Windows 命令都会发生此错误。请就此提出一些解决方案。 谢谢。
最佳答案
您必须使用参数firefox
运行程序start
:
Process process = Runtime.getRuntime().exec("start", "firefox");
start
是一个可执行文件,firefox
也是一个可执行文件,但没有名为 start firefox
的可执行文件。
关于java - 尝试运行java程序来运行Windows命令,但程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711956/