【问题标题】:exit the process once Jar is completedJar 完成后退出进程
【发布时间】: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


【解决方案1】:

根据你的cmets,你想打电话给Process.destroy()

pr.destroy(); // <-- this will "kill" the running program...
              // I'm not sure what it did to you.

【讨论】:

    【解决方案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;
                }
            }
        }
    

    【讨论】: