Shell 脚本不会从 Java 程序运行
有两件事需要立即注意....
- shell 脚本手动运行良好
- 我编写的一个简单的 shell 脚本(echo hello)通过 java 运行良好
所以我有一个 shell 脚本,我试图通过Java进程。
File sqlF = new File("path to deploy script");
Process proc = rt.exec(sqlF + "/deploy.sh");
proc.waitFor();
System.out.println(proc.exitValue());
当我运行此代码时,我得到一个不明确的返回值“1”。
这是 shell 脚本(因为我想问题可能源于这里):
#!/bin/bash
mysql -u XXXX -h XXXXX < XXXXX.sql
mysql -u XXXX -h XXXXX database < DEPLOY-HELPER.sql
关于为什么这不能从 Java 正确执行的任何想法?
There are two things to note right off the bat....
- The shell script runs fine manually
- A simple shell script (echo hello) that I wrote runs fine through java
So I have a shell script that I'm attempting to run through a Java process.
File sqlF = new File("path to deploy script");
Process proc = rt.exec(sqlF + "/deploy.sh");
proc.waitFor();
System.out.println(proc.exitValue());
When I run this code I get an ambiguous return value of "1".
Here's the shell script (because I imagine the issue may stem from here):
#!/bin/bash
mysql -u XXXX -h XXXXX < XXXXX.sql
mysql -u XXXX -h XXXXX database < DEPLOY-HELPER.sql
Any ideas as to why this would not execute properly from Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要运行 shell 脚本,则必须显式调用 shell 并将脚本名称作为参数传递给它,如
“既不是
Runtime.exec()
也不是ProcessBuilder
” 中所示他们自己知道如何执行 shell 脚本,但只知道如何执行二进制可执行文件。If you want to run a shell script you must explicitly invoke the shell and pass it the name of the script as an argument, as in
Neither
Runtime.exec()
norProcessBuilder
know how to execute shell scripts themselves, they only know how to execute binary executables.