从java代码运行脚本shell

时间:2016-08-16 15:14:54

标签: java linux bash shell

我在下面有这个脚本(称为compute.sh,用于启动ROOT应用程序(CERN)(root.exe)并用它执行一些c ++宏)

#!/bin/bash
ROOTSH=/path/to/files root -b -l macro.cpp
$ROOTSH &
ROOT_PID=$!
echo $ROOT_PID > .lock

为了从同一Linux服务器上的java部署应用程序启动此.sh,我使用:

try{  
    Process p = Runtime.getRuntime().exec("/path/to/files/compute.sh");
    p.waitFor();
}catch( IOException ex ){
    log.warn("Cannot find bat or sh to start ROOT");
}catch( InterruptedException ex ){     
    log.warn("The ROOT execution is interrupted");
}

但是当我尝试从UI启动此.sh时,我什么也没有,没有结果也没有错误。 当我从java代码外部运行.sh时,它运行得很好。

有人知道如何处理这个问题吗? 或者是否有其他方法可以确保从java代码运行shell脚本?

1 个答案:

答案 0 :(得分:0)

您的compute.sh脚本可能在您不知情的情况下输出错误。您应该捕获输出和错误流。以下帖子应该对此有所帮助:ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread

相关问题