2012-01-03 132 views
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);} 
} 
} 

上述程序拋出異常如下:麻煩的ProcessBuilder

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

回答

1

您應該包括完整路徑可執行文件(包括目錄和擴展名爲.exe)。

實際上應該是從你得到了錯誤信息明顯:-)

(原因"notepad"曾表示它將搜索%PATH%並嘗試添加.exe如果必要的。這使我相信,"calc"也可能工作:-)

+0

是的它的工作:) – 2012-01-04 04:33:49