Run *.exe file from inside JAR

10,436

Solution 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.

Solution 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.

Share:
10,436
Jaanus
Author by

Jaanus

Doing C#, Java 50-50. SOreadytohelp

Updated on June 04, 2022

Comments

  • Jaanus
    Jaanus almost 2 years

    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?

  • noahlz
    noahlz over 11 years
    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).
  • Link19
    Link19 over 11 years
    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.