1
// following code works fine n open notepad...   
class demo
{
public static void main(String args[])
{
    try{
    ProcessBuilder pb=new ProcessBuilder("notepad");
    pb.start();
    }catch(Exception e)
    {System.out.print(e);}
}
}
 //however the above code throws an exception when any other system program is executed
class demo
{
public static void main(String args[])
{
    try{
    ProcessBuilder pb=new ProcessBuilder("calculator");
    pb.start();
    }catch(Exception e)
    {System.out.print(e);}
}
}

上述程序抛出以下异常:

java.io.IOException: Cannot run program "Calculator": CreateProcess error=2, The system cannot find the file specified
4

1 回答 1

1

您应该包含可执行文件的完整路径(包括目录和 .exe 扩展名)。

从您收到的错误消息中实际上应该很明显:-)

"notepad"起作用的原因表明它会搜索并在必要时%PATH%尝试附加。这让我相信这也可能起作用:-).exe"calc"

于 2012-01-03T07:37:17.080 回答