Reputation: 1529
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
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