【发布时间】:2023-08-10 01:47:02
【问题描述】:
我正在使用进程构建器在我的 Java 程序中使用 shell 脚本执行我的 Runnable Jar。 下面是我的Java代码
command = "t4essh -l " + username + " -p " + password + " -a 2500 -A password " + host + " \" cd " +"/data/jblext/JBLOADER.OUT" +";./test.sh "+tablename+" \"";
Runtime rtt = Runtime.getRuntime();
Process pr = rtt.exec(command);
OutputStream obj = pr.getOutputStream();
ByteArrayOutputStream byte1=new ByteArrayOutputStream();
obj.write(byte1.toByteArray());
String s=byte1.toString();
System.out.println("output from .sh " + s);
System.out.println("command"+ command);
boolean processFinish = false;
while(!processFinish) {
try {
Thread.sleep(500);
int exitVal = pr.exitValue();
System.out.println("Process"+exitVal);
System.out.println("Process exitValue: " + exitVal);
if (exitVal == 0)
{System.out.println("testet");
processFinish = true;}
else
{System.out.println("sysysyyyyyyyy");
processFinish = false;
}
} catch (Throwable t) {
processFinish = false;
System.out.println("catch"+ t);
/* GENERAL.println("Waiting for process finish : " + command);
System.out.println("Waiting for process finish : " + command);*/
// t.printStackTrace();
}
}
}
catch (Exception e) {
BufferedWriter bw = null;
System.err.println("Error executing parse command " + e.getMessage());
}
我在 Linux 服务器中有我的 shell 脚本。下面是一段代码
#!/bin/sh
echo "test"
java -jar Manager.jar $1
当我执行我的 java 程序时
catchjava.lang.IllegalThreadStateException: process has not exited
我的 Runnable jar 将运行至少 2 分钟。可运行的 jar 完成后如何退出?
【问题讨论】:
-
致电
pr.waitFor();和System.exit()? -
亲爱的 Elliott,如果我使用 System.exit() 整个 Java 应用程序结束。我想在执行后退出特定的流程构建器。任何想法...
-
是的。使用
pr.waitFor();,它将等待进程结束。它实际上返回一个int,表示您的进程的退出代码。 -
Elliott,pr.waitFor() 等待了很长时间,但没有退出应用程序。
标签: java shell processbuilder exit-code