Embeding JRE inside the EXE

Help
Anonymous
2012-07-30
2013-06-12

  • Anonymous
    2012-07-30

    is it possible to embed the JRE inside the EXE ? and if yes how ?

     
  • Christoph Nahr
    Christoph Nahr
    2012-11-11

    This is not directly possible but there's a simple workaround. The documentation doesn't explain it clearly, but the diagram on the home page gives a hint when it mentions "Installer created by an external installer program". Here's what you have to do.

    1. Copy your desired JRE (the whole thing from the "jre" folder of some JDK/JRE installation) into a subdirectory below your Launch4j output executable. Let's call this subdirectory "jre" for simplicity.

    2. Set the JRE path in the Launch4j configuration to the name of that subdirectory, "jre" in this example. Use a RELATIVE path.

    3. Now when you deploy your program, you must wrap the Launch4j-created executable AND the jre folder in a zip or setup package so both get deployed to the target machine.

    4. When the executable starts, it will search for its JRE in that relative path specified in the Launch4j configuration. Now it can run on a system that doesn't have a JRE installed. Phew!

    Same procedure applies when you want to package arbitrary libraries with your main application JAR: you need to manually package & deploy them alongside the Launch4j-created executable. Launch4j does exactly one thing, and that's providing a launcher for your main JAR. Basically the same thing as a simple batch file launcher, plus some Windows integration extras. Launch4j's "big" feature is to automatically search for matching JREs but if you package your JRE anyway that doesn't apply.

     
  • Tom Brus
    Tom Brus
    2012-12-14

    This is indeed a workaround and it works. But it burdons my user with an extra step: the unzipping of the zip containing the jre and the l4j produced exe. More to explain, more to go wrong.

    Would it be possible to just put the jre dir in the jar that I am building the l4j exe from, just snuggly next to my classes and then have the exe bit of launch4j exe unzip this jre first to some tmp place and then start that jre with my classes.

    That would enable my users to do one download and one double click.

    Is unzipping before running java a big deal? That has to be done in native code of course.

    Just an idea.
    -Tom

     
  • Christoph Nahr
    Christoph Nahr
    2012-12-14

    If you're fine with extracting to a permanent location (such as c:\Program Files) you could use any number of installer utilities, e.g. InnoSetup (http://www.jrsoftware.org/isinfo.php), to wrap your launch4j executable plus the JRE and all other JARs.

    Extracting to a temporary location and then deleting those files after the program closes is trickier. I don't think InnoSetup can do that. You might be able to script something with a general purpose packer such as 7-zip or WinRAR.

     
  • Tom Brus
    Tom Brus
    2012-12-14

    In general I think that would pollute my users machine. I will check out InnoSetup and see if I can find a way…. thanks Chris.

    Still I think it would be a logical, valuable and simple addition to launch4j.

    -Tom