我想从java运行此命令,但我不能这样做。 你能帮我吗? 当我尝试从ssh运行它时,它工作正常。
String cmd =" myprog -t<< EOF 显示版本 EOF&#34 ;;
进程p = Runtime.getRuntime()。exec(cmd);
答案 0 :(得分:1)
您的命令正在写入流程的标准。您可以从Java代码中写入该进程。像,
String cmd = "myprog -t";
String arg = "show version";
try {
Process p = Runtime.getRuntime().exec(cmd);
PrintStream ps = new PrintStream(p.getOutputStream());
ps.println(arg);
ps.flush();
final StringBuilder sb = new StringBuilder();
final InputStream is = p.getInputStream();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}