Java 如何知道weather server已开始使用runtime.getruntime().exec
Hi已经编写了启动服务器的代码 代码如下所示Java 如何知道weather server已开始使用runtime.getruntime().exec,java,linux,Java,Linux,Hi已经编写了启动服务器的代码 代码如下所示 package javacode; import java.io.BufferedReader; import java.io.InputStreamReader; public class ExecuteShellComand { public static void main(String[] args) { ExecuteShellComand obj = new ExecuteShellComand();
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 pro=Runtime.getRuntime.execcommand您可以使用Process.exitValue检索已执行命令的返回值
请看一下javadoc:
对天气的错误使用。你的意思是,hi Uli,你能解释一下我如何在我编写的代码中使用它吗?我添加了一个返回代码的打印输出作为示例。hi Uli,我编写了启动服务器和停止服务器的代码hi Karan,这是什么意思?如果你觉得我的答案有帮助,请投票支持我的答案。如果您认为这是您问题的正确答案,请将其标记为已接受。我建议阅读stackoverflow简介:
p = Runtime.getRuntime().exec(command);
p.waitFor();
System.out.println("Command '"+command+"' finished with return code "+p.exitValue());