我正在使用进程生成器在我的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 罐子将运行至少 2 分钟。可运行罐子完成后如何退出?
System.exit()
退出整个系统,而不是当前进程。我认为,有两种常见的方法可以退出当前进程(我的意思是Method
)。
1. Use `return`
2. Use `throw Exception`
例:
public void doSomething() {
while(your-condition) {
if(isOK()) {
throw new Exception(); -> It will exit from doSomething() method;
throw new MyException(); -> It will exit from doSomething() method;
return; -> It will exit from doSomething() method;
break; -> It will exit while loop;
System.exit(); -> It will exit from the whole system;
}
}
}
根据你的评论,你想调用Process.destroy();
pr.destroy(); // <-- this will "kill" the running program...
// I'm not sure what it did to you.