2012-01-15 8 views
1

私はコンピュータにいくつかのバットファイルを貼り付けようとしています。 私はProcess.execを使ってそれを行っています。 問題は、batファイルが別のプロセスを開始し、プログラムが完了するのを待っていますが、子プロセスが停止していてもプログラムを停止する必要があることです。 ここでは、私がやろうとしていることの例を示します。このコードでは、私はメモ帳を朗読しています。私はnotepdウィンドウを手動で閉じるまでプログラムが止まっています。メモ帳は動作し続けますか?Java - プロセスを起動して忘れる方法

public static Process test3() throws IOException 
    { 
    Process p; 
    try 
     { 
     p = Runtime.getRuntime().exec("notepad"); 
     } 
    catch (IOException e) 
     { 
     return null; 
     } 
     try 
     { 
     p.waitFor(); 
     } 
     catch (InterruptedException e) 
     { 
     Thread.currentThread().interrupt(); 
     } 


    p.getInputStream().close(); 
    p.getOutputStream().close(); 
    p.getErrorStream().close(); 

    return p; 
    } 
+0

http://stackoverflow.com/questions/931536/how-do-i-launch-a-completely-independent-process-from-a-java-program – Mikhail

答えて

0

あなたがWindowsで実行している場合、これを試してみてください。

Runtime.getRuntime().exec("cmd /c start notepad"); 
+3

down-voter、次回コメントを残す! (何が間違っていたの?) – aviad

+0

@aviad:あまりにも真実、誰かが常に理由を伝えなければならない。よろしく –

0

は、これが役立つことがあります

public class ExternalProcess 
{ 
    public static void main(String... args) throws Exception 
    { 
     Process p = Runtime.getRuntime().exec("Notepad"); 
     p.getInputStream().close(); 
     p.getOutputStream().close(); 
     p.getErrorStream().close(); 
     System.exit(0); 
    } 
} 

は単純でSystem.exit(0)を使用します。

大丈夫

0

より柔軟にProcessBuilderクラスを見ることをお勧めします。

関連する問題