67
votes

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
Regular applications via installer or stand-alone executable will never disappear. There's a ton of reasons why a web-based deployment is not a one-size-fits-all solution.SnakeDoc
@SnakeDoc link not working any more. exe4j likely renamed install4j? ej-technologies.com/products/install4j/overview.htmlakauppi
@akauppi here's an updated link: ej-technologies.com/download/exe4j/filesSnakeDoc
@akauppi Install4j builds an installer, with an install wizard and can do a lot of things. exe4j builds an .exe and you can just launch your app without an installation (think Minecraft.exe which just runs).SnakeDoc
Thanks for the clarification. Even so, the link is outdated. Now: ej-technologies.com/download/exe4j/files (note: commercial license needed)akauppi

13 Answers

24
votes

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.

16
votes

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

7
votes

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

5
votes

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

4
votes

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.

3
votes

Netbeans allows a Java SE project to be packaged in a native application installer, embedding a JRE. It's here :https://netbeans.org/kb/docs/java/native_pkg.html :)

1
votes

I know I'm a little late to this party, however it looks like ej-technologies (love their stuff) has a new solution called exe4j which will do exactly as the OP wanted without any funny business needed.

http://www.ej-technologies.com/products/exe4j/overview.html

If you are working in an OpenSource project, they provide free licenses (you just need to contact them). If it's a commercial project, then a license is required... but at $69 USD for a license, it's darn cheap and worth while imho.

From their website:

If you want your own process name instead of java.exe in the task manager
and a user friendly task-bar grouping, exe4j does the job for you.

exe4j helps you with starting your Java applications in a safe way, displaying
native splash screens, detecting or distributing suitable JREs and JDKs,
startup error handling and much more.
1
votes

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

0
votes

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

0
votes

You might take a look at: http://ulibgcj.sourceforge.net/uswt.html It has micro-swt library that make easy to build GUI using SWT and have a look at Eclipse GCJ Builder

0
votes

Install4j is a great tool for this kink of purpose.Also you could check out JSmooth or launch4j-all though I have tried to bundle jres with JSmooth and was not able to..

0
votes

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