2

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?

Community
  • 1
  • 1
Jaanus
  • 16,161
  • 49
  • 147
  • 202

2 Answers2

4

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.

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75
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.

Link19
  • 586
  • 1
  • 18
  • 47
  • This is actually somewhat inaccurate. Many OS's (or shells, anyway) understand that the JAR uses the ZIP format and thus open them with archive tools. In addition, you can configure Windows to attempt to launch JARs with `javaw` when opened. Finally, you could write a bash/shell script that extracts and launches the EXE (or even do so using `System.exec()` - although it would be hacky / platform dependent). – noahlz Sep 21 '12 at 14:10
  • Apologies, the original question, to me, showed a lack of understanding of the reason why you needed to do the extract. I was trying to clarify. – Link19 Sep 21 '12 at 14:17