85

I have Launch4J on my computer and it's a great program. One of its features I'm interested in is the ability to bundle a JRE in the general .EXE file. However, I can't find any documentation that describes how to go about doing this.

How do I bundle a JRE with the EXE? Plus, where do I get a compact, portable JRE to run? The download links on Oracle are for the installer packages.

CodeBunny
  • 1,893
  • 3
  • 20
  • 32
  • 6
    Launch4j allows you to bundle a JRE **with** your exe, so alongside it. You can't use it to embed the JRE in your exe. – THelper Dec 21 '16 at 12:12
  • What does that even mean... – DKIT Nov 12 '20 at 12:23
  • 2
    @DKIT It means that you can ship your program in a folder with your JRE contained, but you can't generate a standalone .exe file using launch4j that automatically includes your JRE. Therefore, you can bundle a JRE with your exe (in the same folder), but you can't embed it into the exe itself. You'll end up distributing a folder to your clients, or you'll end up depending on their system's JRE/JDK. – Zac Feb 25 '21 at 19:01

6 Answers6

87

After some attempts i finally get a workaround to bundle the jre in my application:

I package my app as a zip file with the following folders inside:

containerFolder
|- jre
      |-bin (in bin there is java.exe)
      |-lib
|- cfg (to save the user configuration, but it's not needed)
|- bin (my application with the .exe and the .jar and all the other stuff)

In the xml file of launch4j i configure the jre like this:

<jre>
    <path>../jre</path>
    <opt>-DgvSIG.confDir=../cfg</opt>
</jre>

The trick here is that the path is not to the java.exe file. The path to the jre is relative to the position of the .exe and it should point to one folder before the java.exe file

The jre folder i'm using is just a copy&paste from the jre folder installed on a windows system.

Francisco Puga
  • 20,593
  • 4
  • 43
  • 56
  • 4
    That helped me a lot!The docs are not too clear about this... Works like a charm :-) – Konrad 'ktoso' Malawski Nov 28 '11 at 00:56
  • Hi what is this path ../jre either it is existing path which is in C://..java folder or a new java JRE installer which is downloaded. – RajaReddy PolamReddy Feb 14 '14 at 11:42
  • @RajaReddyPolamReddy is the relative path from your .exe file to the folder where the jre is located within your applicatin – Francisco Puga Feb 15 '14 at 15:43
  • 11
    The question asked how to bundle the JRE *in* the EXE with launch4j. This answer shows how to tell launch4j to use a specific JRE which you have bundled *with* your EXE, but not inside it. This seems to be the best that can be done with launch4j. (Another answer below shows one way to create an installer which will then install both your EXE and the JRE, but the JRE will not be in the EXE.) – Enwired Sep 24 '15 at 21:14
  • How do you copy the jre? – pupeno Apr 04 '18 at 17:20
  • At the time this answer was written i was using an Ant script that builds the exe from the launch4j config file, compile my app, and build the zip. The jre was copied in the ant script to a folder prior to zip all the folder. – Francisco Puga Apr 05 '18 at 13:10
  • 1
    And what about JDK11 that doesn't come with a JRE? – Panagiss May 17 '20 at 20:30
  • @Panagiss [this answer](https://stackoverflow.com/a/61858820/14765128) links to [AdoptOpenJDK](https://adoptopenjdk.net/archive.html?variant=openjdk11&jvmVariant=hotspot) which offers JREs for newer versions of Java like Java 11. Alternatively you can learn about and use the `jlink` tool to build your own JRE. – jhale1805 Apr 10 '21 at 05:58
17

I did the following and it worked for me using ver Launch4j 3.11:

  1. Created a new folder for my application (called for example MyApp).
  2. Copied the jar file from the java project dist folder to the MyApp along with the lib folder.
  3. Created a folder called jre in my application folder MyApp so now MyApp folder contains:
    • MyApp.jar
    • lib <- this has the libraries needed by my java app.
    • jre
  4. Copied the bin and lib folders from java jre folder (C:\Program Files (x86)\Java\jre7) to MyApp\jre
  5. In the Launch4j having set all the required options, then set the Bundled JRE path to "jre"

    Launch4j JRE options

  6. Make sure that in the search options "Only use private JDK runtimes" is selected.
5377037
  • 9,493
  • 12
  • 43
  • 73
Ahmed Awad
  • 171
  • 1
  • 4
6

The same problem like you mate. No worries now. Its all solve with the maximum depth to solve future solution. Solution how you can bundle your JRE for your jar without the need that the user has to install java or not. Your java application will run.

  1. Copy lib and bin folder from your JRE folder to your project dist folder

jar to exe conversion

  1. open launch4j and enter the following setting. enter image description here

The trick is you need to give the full path to the including javaw.exe.

Enjoy!!!!

Daniel Adenew
  • 6,825
  • 6
  • 48
  • 70
  • 1
    Put any file in dist folder making this error during clean and build: Unable to delete file... – ParisaN Mar 13 '18 at 20:51
0

I have just done this. Above clearly describe the method for bundling jre.

Here, I just share an experience that I have struggled. If you want to create an installer exe after created wrapper exe by launch4j, pay attention to the file path for launch4j and jre. This is my path I used to solve my issues:

launch4j, bin/jre, resources/bin/jre.

launch4j, bin, and resources are at same level.

Xloon
  • 56
  • 7
0

A working example of using Launch4J to bundle a Java application with a specific JRE can be found at https://github.com/vZome/vzome/blob/master/desktop/platform/windows/README.md. This particular distribution is configured to require vZome to use the bundled JRE rather than any JRE found on the target platform.

Hope this helps.

David
  • 255
  • 1
  • 10
-2

The jre can usually be found in your SDK folder. Yes the links online are installers, but once it installs, the JRE is now located on your local disk. Mine is located in

${jdk folder}\jre

The parts that you don't need from the JRE could probably be removed manually if you really wanted (I'm not sure whats available online).

hbhakhra
  • 3,860
  • 1
  • 18
  • 36
  • How do I bundle it into the EXE? I'm trying to package everything in the jar so that I wouldn't have to worry if the user has Java installed. I realize that just having them download java is probably the best idea; however, I'd like to experiment with all of my deployment options. – CodeBunny Aug 17 '11 at 12:16
  • 3
    @CodeBunny This is not possible with Launch4j alone, but somebody found another nice solution: http://mark.koli.ch/bundle-java-the-jre-and-launch-a-java-app-with-7zip-sfx – Zero3 Oct 04 '14 at 23:08