Embed a JRE in a Windows executable?

47,156

Solution 1

Try to use Avian and ProGuard toolkits. Avian allows to embed lightweight virtual machine in you app. Linux, MacOS, Windows and iOS are supported. And ProGuard allows you to shrink large jar file to prepare to embed.

Solution 2

There's a new tool that does this: Packr
https://github.com/libgdx/packr

Solution 3

Quite a few options around, commercially this is very good:

http://www.excelsior-usa.com/jet.html

There are two key open source options GCJ and VMKit

http://gcc.gnu.org/java/ and http://vmkit.llvm.org/

Other more prolonged paths would include IKVM.NET:

http://www.ikvm.net/

Which is a .NET JVM which can be statically compiled to an EXE

Solution 4

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

Solution 5

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.

Share:
47,156
perp
Author by

perp

Long-time hobbyist turned professional in 2009. Started out professionally as a Java/Swing developer, but lately most work has been web development using C# and the ASP.NET MVC stack. Currently learning Biztalk.

Updated on July 05, 2022

Comments

  • perp
    perp almost 2 years

    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)?

  • perp
    perp over 14 years
    Where does it say that? I browsed through it, and all I can see mentioned is that it supports a "bundled" JRE, which isn't the same thing. All I could see on the subject was what's mentioned in section 3.4.2 in the manual...
  • perp
    perp over 14 years
    I've downloaded a trial version, but I can't really figure out how to make it do what I want. All I can make it do is create an installer?
  • perp
    perp over 14 years
    Excelsior JET looks kind of interesting. Will give it a closer look. I'm afraid I don't trust GCJ enough, but I could give it a try. Re: VMKit, wouldn't a JRE implementation for CLI require .NET?
  • matbrgz
    matbrgz over 14 years
    "Sometimes it's more convenient to bundle a JRE with your application. JSmooth can deal with that too, you just need to define in which folder the JRE is expected. It falls back nicely to a standard JVM search if the JRE is not where it should be." I read this as being an unpacked JRE, not an installer.
  • perp
    perp over 14 years
    Yes, exactly, the .exe can look for a JRE in a specified folder, but I already knew that was possible with several different tools. I'm wondering if there's a solution where the JRE can be embedded inside the application binary itself, which is different. So not "bundle with" but "embed in". :-)
  • metismo
    metismo over 14 years
    VMKit is both a .net and JVM solution in compilation terms, for the JVM side I think it merges in GNU Classpath; but its in beta for the moment, so I think its a little off commercial usage. (The only thing I found limiting with Excelsior JET was no MacOSX support.)
  • Jonik
    Jonik over 14 years
    @perp Maybe you can get started by looking at "Step 3: Configure Launchers" in the reference manual: resources.ej-technologies.com/install4j/help/doc/…. Other than that, I'm not sure, as personally I've just created installers. If the manual doesn't help, you could ask their support (which I know to be very good): ej-technologies.com/support/supportRequest
  • Jonik
    Jonik about 14 years
    I just realised that the company behind install4j also has a product called exe4j specifically for creating launchers: ej-technologies.com/products/exe4j/overview.html. I'm not 100% right now if exe4j can do something that install4j doesn't – AFAIK both can create exe launchers with bundled JRE.
  • Venky
    Venky about 13 years
    Please check this question and give me a solution in case of JSMOOTH stackoverflow.com/questions/5407178/…
  • matbrgz
    matbrgz about 13 years
    @venka, i have not tried the embedded use case myself, and i found tje service launcher not to work well. If you cannot make it work easily and the author is not responsive I suggest using another launcher.
  • Dmitry Leskov
    Dmitry Leskov about 12 years
    We have free Excelsior JET licenses available for non-commercial use, just in case: excelsior-usa.com/jetfree.html
  • SnakeDoc
    SnakeDoc almost 11 years
    @Jonik install4j is for creating a bundled exe with embedded jre (or use system jre) to install java applications. exe4j builds a stand-alone exe with bundled jre or use system jre that requires no installation... much like the famed minecraft.exe launcher which requires no isntallation... just double-click it and go.
  • Furetto
    Furetto almost 8 years
    The VMKit project is retired
  • sarah.ferguson
    sarah.ferguson over 7 years
    Packr works however it creates a folder structure containing a jre/ folder, an app.exe and your original app.jar. You still need to create an installer to copy this folder structure in your C:\Programs\ folder. Also your .jar is still there, it's not embedded inside an .exe. Beside this it is a nice tool.
  • sarah.ferguson
    sarah.ferguson over 7 years
    Netbeans automatically creates an installer using required Innosetup and Wix which must be installed separately. The installer when launched creates a folder structure containing a runtime/ folder, an app.exe and an app/ folder with your original app.jar located in your C:\User\AppData\Local folder and then creates a corresponding start menu. Notice your .jar are still there, it's not embedded inside an .exe
  • jhagege
    jhagege over 6 years
    Did you find a better tool to have a single executable ?
  • Gabriel H
    Gabriel H over 4 years
    check out my answer below it creates a single executable