处理IllegalThreadStateException [英] Handling the IllegalThreadStateException

查看:1483
本文介绍了处理IllegalThreadStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在学习如何在java中执行系统命令发生此错误。我尝试使用抛出来否定它但没有用。请解释原因和解决方案

i was learning how to execute system commands in java and this error occured. i tried using throws to negate it but was of no use. 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)


推荐答案

调用 处理#waitFor() 。这将阻止当前线程,直到生成的进程终止。如果不这样做,请 流程#exitValue() 抛出


IllegalThreadStateException - 如果此
Process 对象所代表的子流程尚未终止

IllegalThreadStateException - if the subprocess represented by this Process object has not yet terminated

这篇关于处理IllegalThreadStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆