关于一JAVA程序运行另一程序的输出问题,555,多给些分,帮忙啊~~~~

acute acute 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我有一段SHELL代码,名字叫做test.sh。
运行命令
sh test.sh
会在屏幕上打印出haha字符串。

我在UNIX机器中,用如下命令运行,让它在后台运行,不输出,但是要得到它在后台的进程号,可以用如下命令
nohup sh test.sh >/dev/null&
则会在屏幕上输出它的进程号,如:
[1] 3403

问题来了,我要在一个程序中运行这个test.sh,同样输出进程号,我有如下代码

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class test {
public test() {
super();
}

public static void main(String[] args) {
try {
String cmd = "nohup sh test.sh >/dev/null&";
Process child = Runtime.getRuntime().exec(cmd);
InputStream child_in = child.getInputStream() ;
BufferedReader inbr=new BufferedReader(new InputStreamReader(child_in));

String line;
line=inbr.readLine();
while (line!=null)
{
System.out.println(line);
line=inbr.readLine();
}
child_in.close();
child.destroy() ;
} catch (IOException e) {
System.err.println(e);
}

}
}


问题!问题!他没给输出进程号,而是"haha"!!
可是,我的cmd明明是"nohup sh test.sh >/dev/null&"啊!!!!
...全文
评论 10
收藏 已收藏
分享
返回顶部
acute
Java SE 62,620 成员 307,251 内容