如何知道天气服务器已经开始使用runtime.getruntime()。exec

时间:2015-02-28 14:13:47

标签: java linux

您已经编写了启动服务器的代码。

代码如下所示。

package javacode;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExecuteShellComand {

    public static void main(String[] args) {
        ExecuteShellComand obj = new ExecuteShellComand();
        String command = "command to be executed to run the server(Path)";
        String output = obj.executeCommand(command);
        System.out.println(output);
    }

    private String executeCommand(String command) {
        StringBuffer output = new StringBuffer();
        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            BufferedReader reader =
                           new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";           
            while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }

}

当我运行java代码时服务器已在后台启动,但我使用linux命令检查服务器已启动的天气。

我想知道运行时Process对象中存储了什么返回值 Process pro = Runtime.getRuntime()。exec(command)

1 个答案:

答案 0 :(得分:0)

您可以使用process.exitValue()来检索已执行命令的返回值。

请查看javadoc:http://docs.oracle.com/javase/8/docs/api/java/lang/Process.html#exitValue--

    p = Runtime.getRuntime().exec(command);
    p.waitFor();
    System.out.println("Command '"+command+"' finished with return code "+p.exitValue());