Embed a JRE in a Windows executable?

Viewed 48697

Suppose I want to distribute a Java application.

Suppose I want to distribute it as a single executable. I could easily build a .jar with both the application and all its external dependencies in a single file (with some Ant hacking).

Now suppose I want to distribute it as an .exe file on Windows. That's easy enough, given the nice tools out there (such as Launch4j and the likes).

But suppose now that I also don't want to depend on the end user having the right JRE (or any JRE at all for that matter) installed. I want to distribute a JRE with my app, and my app should run on this JRE. It's easy enough to create a Windows installer executable, and embed a folder with all necessary JRE files in it. But then I'm distributing an installer and not a single-file app.

Is there a way to embed both the application, and a JRE, into an .exe file acting as the application launcher (and not as an installer)?

13 Answers

After encountering the issue myself -

1)create a folder containing your application jar file

2)create a subfolder containing the jre

3)create a bat file that overwrites env variables for the duration of application usage and will launch your application:

REM requiered so java wont run into issues with an installed version if one exists
SETLOCAL ENABLEEXTENSIONS
SET JAVA_HOME="./jre"
"./jre/bin/java.exe" -jar "applicationName.jar"
pause

4)use bat to exe converter options include: https://listoffreeware.com/6-best-free-bat-exe-converter-software-windows/

the first option is avaliable here:

https://web.archive.org/web/20190305143030/http://www.f2ko.de/downloads/Bat_To_Exe_Converter.zip

5)create an exe that contains the folder and runs the bat file when run

Is there a way to embed both the application, and a JRE, into an .exe file acting as the application launcher (and not as an installer)?

If a commercial tool is ok for you, install4j solves this problem very smoothly (see its "Features" page). It can generate both application launchers and installers.

I'd hate to repeat myself too much, so check e.g. this earlier answer where I plugged it (as installer builder, but it doesn't make much difference here).

Summary / bottom line: install4j can create native .exe launchers that absolutely do not depend on a pre-installed JRE (or any other specific libs), and it offers flexible options for bundling (and detecting) JREs.

I wrote a solution that has the following:

  • it compiles to just one small exe-file (30 kb) that only calls the jre
  • you compile the exe once from a standalone cpp file (you need to modify the cpp file, replacing "com.example.MyApplication" with your own class name)
  • choose an icon, it will be embedded in exe file
  • the finished exe file is now your java launcher, which catches any errors of java.exe, and shows an information dialog if launch fails
  • no console window

Since the exe file is so tiny, you can build it once and put it the repo. You won't need to update it unless you rename the main Java class or want to change icon.

Your deployed application would look something like this:

app_root/
    myapp.exe        # the launcher created by this project
    jre/             # the bundled JRE, created by jlink
    lib/
        MyApp.jar    # your Java application
        ...          # any third-party libraries, etc.

You can check it out here: https://github.com/foolo/windows-jre-laucher

JSmooth can do this according to the documentation. I have only tried it without an embedded JRE, but been very satisfied. It is scriptable with ant and we build on Linux.

http://jsmooth.sourceforge.net/features.php

I found Engima Projector x86 / x64 better than BoxedApp Packer

If you get soft miminizejre by packr than copy your application myapp.jar into launcher\jre\bin\

and copy from installed jre example C:\Program Files\Files\Java\1.8.0_xxx\bin\javaw.exe to launcher\jre\bin\

Open Engima Protector input -> javaw.exe from launcher\jre\bin\

And go Options if you want high compress and Find Miscellaneous -> Command line "-jar myapp.jar"

And click protect and wait for whole dlls and jar into exe

Make sure your myapp.jar should generate by Eclipse with "Package required libraries into generated JAR" because you don't worry if you have imported many libraries example lwjgl or JavaFX than you must check if embedded javaw_protected.exe has included important files example assets.

But BoxedApp Packer is same too BoxedApp Packer is bit bigger than Engima Protector. Protector is almost best small embedding javaw.exe with assets / resources. Than they can't crack jar files... I hope you are happy with own jar into exe as less 4 mb without lib directory - If you have problem with jvm.cfg error message It means you don't copy embedded exe into root directory of jre or jdk

Please make sure embedded exe is here outside of jre or jdk root directory.

I hope you haven't problem if you use Protector x86 than it need use i586, Protector x64 for jre x64

If you use Protector x64 with jre x86/i386 - It is okay no problem. It works fine.

Best regards

// EDIT:

UPDATED NEW VERSION EXCELSIOR JET + VIRTUAL BOX OR PROTECTOR are almost best "embedded applications"

Check youtube: https://www.youtube.com/watch?v=ctbIxq-1MGE

Related