Java 如何让 runtime.getruntime().exec(String command) 返回一个布尔值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22551601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:26:23  来源:igfitidea点击:

how to make runtime.getruntime().exec(String command) return a boolean value?

javabatch-file

提问by Sandeep

I am trying to use runtime.getruntime().exec(String command) to return a value so that I can make the system decide on executing it again or not. Is there a solution for this?

我正在尝试使用 runtime.getruntime().exec(String command) 返回一个值,以便我可以让系统决定是否再次执行它。有解决方案吗?

回答by MadProgrammer

Runtime.execreturns a Process, process has a method called waitFor, which will wait for the running process to terminate and return.

Runtime.exec返回一个Process,进程有一个方法叫做waitFor,它会等待正在运行的进程终止并返回。

Processhas a method called exitVaue, which returns an intreturning the exit state of the program. Convention suggests that 0is an indication of a normal termination, but this might be contextual to the program you are running.

Process有一个方法叫做exitVaue,它返回一个int返回程序的退出状态。约定表明这0是正常终止的指示,但这可能与您正在运行的程序有关。

You would need to...

你需要...

  1. Know what is a valid exit value for the process your are running
  2. Check the exitValuereturned by the instance of Processagainst the known valid exit values.
  1. 知道您正在运行的进程的有效退出值是什么
  2. 根据已知的有效退出值检查exitValue实例返回的Process

Things to note...

注意事项...

  • Generally, you should be consuming the InputStreams (input and error) of the Process, as failing to do so can cause some processes to stall
  • ProcessBuilderis generally a better solution as has better functionality when it comes to dealing with multiple parameters for the command, has redirection and can even determine the starting directory context for the command...
  • 通常,您应该使用 的InputStreams(输入和错误)Process,因为不这样做会导致某些进程停顿
  • ProcessBuilder通常是更好的解决方案,因为在处理命令的多个参数时具有更好的功能,具有重定向功能,甚至可以确定命令的起始目录上下文...

回答by Shriram

Runtime runtime = Runtime.getRuntime();
       Process process = runtime.exec(args);
       InputStream is = process.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);
       }

回答by Vivek

it returns Process that is what it will return , it up to you to deduce if it ran successfully or not and that depend on what your process is actually doing.

它返回 Process 这就是它将返回的内容,由您来推断它是否成功运行,这取决于您的流程实际在做什么。

回答by Haroon

runtime.getruntime().exec(String command) method create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process and It has one method exitValue and it returns 0 mean it's a normal termination

Reference :http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html 

runtime.getruntime().exec(String command) 方法创建一个本机进程并返回一个 Process 子类的实例,该子类可用于控制进程并获取有关它的信息。Process 类提供了从进程执行输入、执行输出到进程、等待进程完成、检查进程退出状态和销毁(杀死)进程的方法,它有一个方法 exitValue 并返回 0意味着这是一个正常的终止

Reference :http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html