关于一JAVA程序运行另一程序的输出问题,555,多给些分,帮忙啊~~~~
acute 2003-03-10 04:32:09 我有一段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&"啊!!!!