2
我试图得到一个shell/bash脚本的输出,即从Java程序运行,但我运气不好,代码如下:运行的bash脚本并返回结果
GetStats(winhostname);
public static String winhostname "cmd /c hostname";
public static void GetStats(String operation)
{
try
{
Process p=Runtime.getRuntime().exec(operation);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
if (operation.equals("winhostname"))
{
winhostnamevalue = line;
}
line=reader.readLine();
}
}
catch(IOException e1) {}
catch(InterruptedException e2) {}
}
这适用于Windows,因此我将winhostname的值更改为“sh namecheck.sh”(它简单地回显了主机名),并且shell脚本与java/class文件位于同一目录中。虽然运行时我得到一个空白的结果,不是空的,只是空白。
你正试图在Windows上运行bash shell? – hvgotcodes 2012-02-02 15:54:50
@hvgotcodes我希望不会:P – Mike 2012-02-02 16:00:03
尝试抓住你的unix进程的stderr - 它可能指向你正确的方向 - 请参阅Process#getErrorStream() – DaveH 2012-02-02 16:11:19