JavaGeek
JavaGeek

Reputation: 1529

unable to kill a process using Process.destroy()

I'm just experiminting with Runtime and Process classes of java. I'm trying to open a application like windows word using Runtime.exec() and then waiting some time and trying to destroy it using Process.destroy() method. MS Word is opening but it's not closing throwing below exception in console

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

Below is my code

 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();  
    }

}

Upvotes: 1

Views: 830

Answers (1)

Costi Ciudatu
Costi Ciudatu

Reputation: 38235

The problem here is that you cannot invoke Object.wait() without holding the monitor for that object:

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

Upvotes: 3

Related Questions