Java – handling illegalthreadstateexception

import java.io.*;
import java.io.*;
class Sysexecute
{
    public static void main(String args[]) throws IOException,InterruptedException,IllegalThreadStateException
    {
        Runtime rt= Runtime.getRuntime();
        Process p=rt.exec("ls");
        System.out.println(p.exitValue());
    }
}

I'm learning how to execute system commands in Java and this error occurs I tried to use throw out to deny it, but it didn't work Please explain the reason and solution

actual error:-
Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
    at java.lang.UNIXProcess.exitValue(UNIXProcess.java:270)
    at Sysexecute.main(Sysexecute.java:8)

Solution

Call Process#waitFor () before trying to get the exit value. This will block the current thread until the generated process terminates If you don't, process #exitvalue() will throw

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>