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 に答える