简体   繁体   English

将项目导出为可运行的jar时无法调用外部Bash程序

[英]Unable to call external Bash program when exporting project as runnable jar

I have a java project that requires an external bash script to work. 我有一个需要外部bash脚本才能工作的java项目。 It works perfectly fine in Eclipse, but once I export it as a runnable jar, I get the following error: Cannot run program "src/script.sh": error=2, No such file or directory 它在Eclipse中运行良好,但是一旦将其导出为可运行的jar,就会出现以下错误:无法运行程序“ src / script.sh”:error = 2,没有这样的文件或目录

I have tried: 我努力了:

Runtime.getRuntime().exec("script.sh");

As well as 以及

new ProcessBuilder("src/script.sh").start();

The Bash script takes in no input and does not return any output Bash脚本不接受任何输入,也不返回任何输出

Your script is buried inside the jar file and doesn't exist as a standalone file, that's why Java can't "execute" it. 您的脚本埋在jar文件中,并且不作为独立文件存在,这就是Java无法“执行”它的原因。 You need to read it at runtime using the resources API, then save it to some (temporary?) directory, set its executable permission, and only then run it using ProcessBuilder or some such. 您需要在运行时使用资源API读取它,然后将其保存到某个目录(临时目录),设置其可执行权限,然后再使用ProcessBuilder或类似的程序运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM