70

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

Jonik
  • 80,077
  • 70
  • 264
  • 372
perp
  • 3,883
  • 4
  • 31
  • 29
  • 15
    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 Aug 08 '13 at 15:19
  • @SnakeDoc link not working any more. exe4j likely renamed install4j? http://www.ej-technologies.com/products/install4j/overview.html – akauppi Apr 23 '15 at 13:06
  • @akauppi here's an updated link: http://www.ej-technologies.com/download/exe4j/files – SnakeDoc Apr 23 '15 at 15:01
  • @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 Apr 23 '15 at 15:02
  • Thanks for the clarification. Even so, the link is outdated. Now: https://www.ej-technologies.com/download/exe4j/files (note: commercial license needed) – akauppi Apr 23 '15 at 15:21

13 Answers13

24

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.

muton
  • 382
  • 2
  • 9
i_home
  • 264
  • 2
  • 3
17

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

Andrejs
  • 26,885
  • 12
  • 107
  • 96
  • 3
    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 Feb 16 '17 at 08:59
  • Did you find a better tool to have a single executable ? – jhagege Sep 13 '17 at 11:33
  • check out my answer below it creates a single executable – Gabriel H Feb 07 '20 at 11:36
7

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

metismo
  • 457
  • 2
  • 6
  • 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? – perp Feb 13 '10 at 21:44
  • 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.) – metismo Feb 14 '10 at 00:31
  • We have free Excelsior JET licenses available for non-commercial use, just in case: http://www.excelsior-usa.com/jetfree.html – Dmitry Leskov Feb 21 '12 at 04:09
  • The VMKit project is retired – Furetto Jul 05 '16 at 06:51
6

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

Gabriel H
  • 1,558
  • 2
  • 14
  • 35
4

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.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • 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 Feb 13 '10 at 21:36
  • @perp Maybe you can get started by looking at "Step 3: Configure Launchers" in the reference manual: http://resources.ej-technologies.com/install4j/help/doc/indexRedirect.html?http&&&resources.ej-technologies.com/install4j/help/doc/steps/launcher/launcher.html. 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): http://www.ej-technologies.com/support/supportRequest – Jonik Feb 14 '10 at 11:17
  • I just realised that the company behind install4j also has a product called **exe4j** specifically for creating launchers: http://www.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. – Jonik May 02 '10 at 12:35
  • @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. – SnakeDoc Aug 08 '13 at 15:29
3

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

TianaR
  • 119
  • 1
  • 4
  • 2
    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 – sarah.ferguson Feb 16 '17 at 10:15
1

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.
SnakeDoc
  • 13,611
  • 17
  • 65
  • 97
1

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

foolo
  • 804
  • 12
  • 22
0

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

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 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 Feb 13 '10 at 21:10
  • "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. – Thorbjørn Ravn Andersen Feb 13 '10 at 22:01
  • 2
    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". :-) – perp Feb 13 '10 at 22:50
  • Please check this question and give me a solution in case of JSMOOTH http://stackoverflow.com/questions/5407178/problem-in-bundling-jre-in-jsmooth-in-java – Venky Mar 23 '11 at 15:34
  • @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. – Thorbjørn Ravn Andersen Mar 23 '11 at 16:02
0

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

SourceSkyBoxer
  • 141
  • 1
  • 8
0

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

Kachwahed
  • 542
  • 7
  • 17