Run *.exe file from inside JAR

守給你的承諾、 提交于 2019-11-27 07:19:02

问题


I have a huge JAR file, which I created with Maven Shade plugin.

I run this with java -jar foo.jar , and my UI opens. Now I want to execute *.exe file, which is also in that JAR file, how can I do this?

I tried putting the exe to my classpath and run it from there, but after trying I found out that classpath is actually the location, where my JAR is.

Any suggestions?

Found this thing here, but is this really the best solution? Seems like alot of work, I think I have different case here, because I can define the location of exe myself and the JAR is created by me.

run exe which is packaged inside jar

Why I need this?

I want to give user a single file executable, which he can run, but my program is using the *.exe. Should I put the exe next to my jar and there will be 2 files or there is solution for my requirements?


回答1:


Copying the file to a temporary location and running it is the way to go. The answer you linked to does much more work that necessary, as you can get your exe file as an InputStream and copy it to a file with a utility like Apache Commons IO FileUtils.copy(in, out)

See How do I copy a text file from a jar into a file outside of the jar? for example.




回答2:


It's not about the location, it's about the fact that you need to tell your OS to run the exe and, unfortunately, you can't do that by providing a location within a jar.



来源:https://stackoverflow.com/questions/12531766/run-exe-file-from-inside-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!