使用ProcessBuilder时出现问题

时间:2012-01-03 07:34:26

标签: java runtime.exec processbuilder

// 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

1 个答案:

答案 0 :(得分:1)

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

从您收到的错误消息中可以明显看出: - )

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