相关疑难解决方法(0)

如何从Java中的Process运行中获取bash命令退出代码?

我有一个程序是:

import java.io.*;
   import java.util.*;

   public class ExecBashCommand {
     public static void main(String args[]) throws IOException {

       if (args.length <= 0) {
         System.err.println("Need command to run");
         System.exit(-1);
       }

       Runtime runtime = Runtime.getRuntime();
       Process process = runtime.exec("./nv0914 < nv0914.challenge");
       Process process1 = runtime.exec("echo ${?}");
       InputStream is = process1.getInputStream();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);
       String line;

       //System.out.printf("Output of running %s is:", Arrays.toString(args));

       while ((line = br.readLine()) != null) {
         System.out.println(line);
       }

     }
    } 
Run Code Online (Sandbox Code Playgroud)

注意:nv0914是一个bash可执行文件,nv0914.challenge是一个文本文件.当我在终端中运行任何正常命令时,如果我使用后检查退出代码echo ${?}.现在我想通过使用程序来做同样的事情,但程序只是提供输出 …

java unix bash runtime.exec exit-code

5
推荐指数
1
解决办法
7064
查看次数

标签 统计

bash ×1

exit-code ×1

java ×1

runtime.exec ×1

unix ×1