英文:
How to execute a shell script in Java
问题
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("sh", "-c", script);
processBuilder.redirectErrorStream(true);
// start() will be in try-catch
process = processBuilder.start();
int exitVal = process.waitFor();
if(exitVal == 0){
//code
}
List<String> command = Arrays.asList("ksh", "/xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh", "param1", "param2", "20200901", "459", "121");
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true);
// start() will be in try-catch
process = processBuilder.start();
int exitVal = process.waitFor();
if(exitVal == 0){
//code
}
ProcessBuilder processBuilder = new ProcessBuilder("Notepad.exe", "C:/Dev/Test.txt");
processBuilder.redirectErrorStream(true);
process = processBuilder.start();
Linux执行脚本的部分已进行适当修改。
英文:
I have to run a shell script from Java in a Linux box
for eg,
Below is the script path and all parameters needed by the script.
"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
}
Do i need to pass the script as a List<String> by adding each as a separate String in the List ?
"ksh" , "/xyz/abc/data/code_base/RUN_SCRIPTS/dev/my_script.sh" , "param1" "param2" "20200901" "459" "121"
On my Windows machine, i am opening a Notepad as below and it works fine,
ProcessBuilder processBuilder = new ProcessBuilder("Notepad.exe", "C:/Dev/Test.txt");
processBuilder.redirectErrorStream(true);
process = processBuilder.start();
Will the script get executed on the Linux box properly or any more changes are required in the code ??
答案1
得分: 1
你应该能够使用Runtime.exec()
或ProcessBuilder
来调用shell运行脚本,但整个过程充满了陷阱。
我建议阅读这篇文章:
https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html
该文章解释了许多这些陷阱,以及如何避免它们。特别是在Linux上,您几乎肯定需要从shell调用中获取stdout和stderr输出 - 即使脚本不产生任何输出,这一点有时也成立。您还需要小心处理参数中的空格和特殊字符 - 如果有的话 - 在您的脚本中。Java用于命令行的解析器盲目地在空格处分割命令行。
英文:
You should be able to use Runtime.exec()
or ProcessBuilder
to invoke a shell to run a script, but the whole process is full of pitfalls.
I would suggest reading this article:
https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html
which explains many of these pitfalls, and how to avoid them. In particular, on Linux you'll almost certainly need to consume stdout and stderr from your shell invocation -- that's sometimes true even if the script doesn't produce any output. You'll also need to be careful how you handle spaces and special characters -- if there are any -- in the arguments to your script. The parser that Java uses for the command line blindly breaks a command line at whitespace.
答案2
得分: 0
看看这个 Runtime.getRunTime().exec
英文:
Take a look at this Runtime.getRunTime().exec
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论