Java执行在JAR中捆绑的Shell脚本 [英] Java Execute Shell Script Bundled within JAR

查看:1153
本文介绍了Java执行在JAR中捆绑的Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在jar文件中执行shell脚本。如何提取?

我有一个shell脚本打包到我的JAR文件中'引擎'包。

I have a shell script packaged into my JAR file within the 'engine' package.

在我的程序中,我使用Process和ProcessBuilder运行shell命令。一切正常。

In my program I am running a shell command using Process and ProcessBuilder. This all works fine.

如果我在计算机上指定shell脚本的路径,那么程序运行正常。但是,如果我将shell脚本打包到我的JAR中并像这样访问它:

If I specify the path to the shell script on my computer then the program works fine. If however I package the shell script into my JAR and access it like this:

scriptLocation = this.getClass().getResource("/engine/shell-script.sh").toString();

运行程序,然后我收到以下错误:

And run the program, then I get the following error:

java.io.IOException: Cannot run program "file:/Users/USERNAME/Desktop/Application-Name.jar!/engine/shell-script.sh": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at engine.Download$1.run(Download.java:39)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
    at java.lang.ProcessImpl.start(ProcessImpl.java:91)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 5 more

我开始了nk这不能做,我非常想提供这个shell脚本作为应用程序的一部分,可以这样做吗?

I'm starting to think this cannot be done, I would very much like to provide this shell-script as part of the application, can it be done?

先谢谢大家。

========更新==========

我最终解决了这个问题。 Shell无法在JAR包中执行脚本,它不是Java,而是shell。我通过执行以下操作来修复此问题:

I eventually solved this problem. Shell cannot execute scripts within a JAR bundle, it's not about Java, but shell. I fixed this by doing the following:

Java有一个createTempFile()方法(在应用程序终止后可以选择删除文件),调用此函数并写入JAR包中要访问此临时文件的文件。然后在本地文件系统上有shell脚本,并且能够执行脚本。

Java has a createTempFile() method (with option to delete the file once the application has been terminated), invoke this, and write the file within the JAR bundle that you want to access to this temp file. You then have the shell script on the local filesystem, and are able to execute the script.

推荐答案

它不是关于Java,它的关于壳。据我所知,shell解释器无法执行驻留在zip文件中的脚本。所以你有两个选择:

Its not about Java, its about shell. As far as I know shell interpreter can't execute scripts that reside in the zip file. So you have a couple of options here:


  1. 从你的java程序中读取shell脚本作为文本文件。
    创建一个临时文件(在临时目录或任何其他相关位置并复制那里的文件内容。然后用这个时间脚本调用shell解释器。
    我相信它是最好的方法

  1. Read the shell script as a text file from within your java program. create a temporal file (in temp dir or any other relevant place and copy the content of the file there. And then call the shell interpreter with this temporal script. I believe its the best approach

由于jar是一个zip,你可以从shell中解压缩它找到一个脚本并执行shell。再次这更尴尬而且错误,但从技术上来说应该是

Since jar is a zip, you can unzip it from within the shell find a script and execute the shell. Again this is more awkward and rather wrong, but technically it should work.

如果您的脚本中没有低级别的东西,您可以考虑将逻辑重写为groovy脚本(也许您会发现有用的groosh项目)。
然后从内存中调用groovy代码,而不是从文件中调用。

If you don't have low level stuff in your script, you can consider to rewrite the logic to groovy script (maybe you'll find useful groosh project). Then call the groovy code from the memory and not from the file.

嗯,我没有想法,如果我是你,我会实施第一个解决方案:)
祝你好运,希望这会有所帮助

Well, I'm out of ideas, If I were you, I would implement the first solution :) Good luck and hope this helps

这篇关于Java执行在JAR中捆绑的Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆