我有一个程序是:
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 ${?}.现在我想通过使用程序来做同样的事情,但程序只是提供输出 …