2011-07-27 113 views
1

我只是嘗試使用java的Runtime和Process類。我試圖用Runtime.exec()打開一個像windows這樣的應用程序,然後等待一段時間,並嘗試使用Process.destroy()方法銷燬它。 MS Word中被打開,但它不是關閉在控制檯拋出異常下面無法殺死使用Process.destroy()的進程

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

下面是我的代碼

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

這裏的問題是,你不能沒有握住顯示器,該對象調用Object.wait()

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

使用Thread.sleep代替 –

+0

@costi非常感謝..幫助我 – JavaGeek