2011-07-27 4 views
1

저는 java의 Runtime 및 Process 클래스를 실험하고 있습니다. 나는 Runtime.exec()을 사용하여 windows 단어와 같은 응용 프로그램을 열려고 시도하고 나서 약간의 시간을 기다렸다가 Process.destroy() 메서드를 사용하여 그것을 파괴하려고합니다. MS 워드가 개방되어 있지만 콘솔에서 예외 아래로 던지는 닫는 아니에요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

관련 문제