下面是脚本的路径和脚本所需的所有参数。
"ksh /xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh param1 param2 20200901 459 121"
Below is the code,
String script = "ksh /xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh param1 param2 20200901 459 121";
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(script);
processBuilder.redirectErrorStream(true);
//start() will be in try catch
process = processBuilder.start();
int exitVal = process.waitFor();
if(exitVal == 0){
//code
我是否需要通过在列表中添加每一个单独的字符串来将脚本作为一个列表来传递?
"ksh" , "/xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh" , "param1" "param2" "20200901" "459" "121"
在我的Windows机器上,我按下面的方法打开记事本,它工作得很好。
ProcessBuilder processBuilder = new ProcessBuilder("Notepad.exe", "C:/Dev/Test.txt");
processBuilder.redirectErrorStream(true);
process = processBuilder.start();
脚本会在Linux盒子上正常执行,还是需要对代码进行更多的修改?
2 个回答
0 人赞同
You should能够使用Runtime.exec()
或ProcessBuilder
来调用shell来运行一个脚本,但整个过程充满了隐患。