gpt4 book ai didi

java - 处理 IllegalThreadStateException

转载 作者:搜寻专家 更新时间:2023-10-30 21:24:01 25 4
gpt4 key购买 nike

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 中执行系统命令,但出现了这个错误。我尝试使用 throws 来否定它,但没有用。请说明原因及解决方法

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)

最佳答案

调用 Process#waitFor()在尝试获取退出值之前。这会阻塞当前线程,直到生成的进程终止。如果你不这样做,Process#exitValue()抛出

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

关于java - 处理 IllegalThreadStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com