2011-07-27 26 views
1

私はjavaのRuntimeおよびProcessクラスで実験しています。私はRuntime.exec()を使用してWindowsの単語のようなアプリケーションを開き、しばらく待って、Process.destroy()メソッドを使用してそれを破壊しようとしています。 MS Wordを開いているが、それは、コンソールで例外の下に投げて閉じていないProcess.destroy()を使用してプロセスを強制終了できません

以下
exception::java.lang.IllegalMonitorStateException: current thread not owner 

は、ここでの問題は、あなたがそのオブジェクトのモニターを保持せずにObject.wait()を呼び出すことができないということです私のコード

import java.util.*; 

public class StringTesting { 

    public void open() 
    { 

     try{ 
     Runtime runtime = Runtime.getRuntime(); 

     Process proc =runtime.exec("C:\\Program Files\\Microsoft Office\\Office12\\winword.exe"); 

     StringTesting st = new StringTesting(); 

     st.wait(5000); 

     // now destroy the process 

     proc.destroy(); 
     System.out.println(" after destroy"); 
     } 
     catch(Exception e) 
     { 
      System.out.println(" exception::"+e); 
     } 
    } 

    public static void main(String a[]) 
    { 
     StringTesting st = new StringTesting(); 
      st.open(); 
    } 

} 

答えて

3

です:

StringTesting st = new StringTesting(); 
synchronized (st) { 
    st.wait(5000); 
} 
+0

代わりにThread.sleepを使用してください。 –

+0

@costi大変ありがとうございます。助けてくれました – JavaGeek

関連する問題