如何从另一个路径执行脚本
我想从 java 项目执行 myscript.sh
。
我想做的调用是这样的:
Process p = Runtime.getRuntime().exec("./myscript.sh "+param1+" "+param2);
问题是这个 script.sh 不在同一个路径中,所以我尝试这样做:
Process p = Runtime.getRuntime().exec("src/main/resources/./myscript.sh "+param1+" "+param2);
但脚本不再执行。我想问题出在我放置路径的方式上,因为我已经检查过,并且如果脚本位于同一路径中,则脚本可以正常工作。
有什么想法吗?
提前致谢
I want to execute myscript.sh
from a java project.
The call I want to do is something like this:
Process p = Runtime.getRuntime().exec("./myscript.sh "+param1+" "+param2);
The problem is that this script.sh is not in the same path, so I tryed to do:
Process p = Runtime.getRuntime().exec("src/main/resources/./myscript.sh "+param1+" "+param2);
But the script is not executed anymore. I guess the problem is in the way I put the path, because I have checked and the script works perfectly if it is in the same path.
Any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ProcessBuilder 代替。在 Runtime.exec 的文档 您甚至可以阅读以下内容:
如文档中所示的示例,您可以使用
pb.directory(File f)
设置工作目录:You could use the ProcessBuilder instead. In the documentation for Runtime.exec you can even read the following:
As an example shows in the documentation, you can use
pb.directory(File f)
to set the working directory: