trouble with ProcessBuilder

混江龙づ霸主 提交于 2019-12-11 05:41:36

问题


// 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);}
}
}

the above program throws following exception:

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

回答1:


You should include the full path to the executable (including the directories and the .exe extension).

Should actually be apparent from the error message you got :-)

(The reason "notepad" worked indicates that it will search %PATH% and try to append .exe if necessary. This leads me to believe that "calc" may also work :-)



来源:https://stackoverflow.com/questions/8709184/trouble-with-processbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!