39

Does somebody know how to pack a particular JRE together with a compiled Java application? I currently have an executable jar file and wish to put JRE 6 Update 31 in it. So that the application uses the shipped JRE regardless of which JRE version the client has installed. The solution should work platform independent.

Any ideas? Many thanks in advance!

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
salocinx
  • 3,715
  • 8
  • 61
  • 110

9 Answers9

87

If you have the jre installed on a target platform, say Windows 64 bit, simply copy everything under the jre folder in your Java install and place it in your distro. Then create a batch file to point to your local jre instead of the system one.

Here is what I do:

  • my jar file is in a dist folder
  • copy the system jre folder to dist\jre-win-1.7.0_04
  • create a .bat file with the following line in it jre-win-1.7.0_04\bin\java.exe -jar MyProgram.jar

..boom, done! Double click the batch file and it runs your jar file with the local jre. To prove it to yourself, uninstall your system jre and double click the batch file. Still works.

You can do this on Linux or OS X in an analogous way.

I would also like to point out that this is a fairly common practice and many Java distributions are done like this. So, don't say you can't or shouldn't.

There are some caveats, however. This does make your distro larger and platform dependent, in my example Windows x64. However, it's doable and manageable. There are a finite number of platforms supported for the jre and guess what... they're platform dependent too.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Dbominable
  • 879
  • 1
  • 5
  • 2
  • 2
    Packaging everything into the installer and making it platform-dependent is worse than creating one package, and pointing the user to the right JRE, **unless** you are targeting one platform only. Managing lots of different executables is an issue in itself. – Anirudh Ramanathan Nov 17 '12 at 06:40
  • 3
    @DarkCthulhu having one installer for all platforms does not work well for customers, they are only interested inn the installer working well for their given platform, not the same for all platforms. – Paul Taylor Jul 18 '14 at 12:15
  • Thanks for the specifics, @Dbominable and colorful language, too. We're targeting just Windows 64-bit so your encouragement was spot on. – akauppi Apr 22 '15 at 21:07
  • A feet in the real world answer ! Nice :-) But please tell me have you hit any issues catering for both Intel and AMD? I'm looking at doing the same thing myself. – David Brown Jul 13 '15 at 15:30
  • How do you copy the jre as part of your build process? – Pablo Fernandez Apr 05 '18 at 07:45
  • Thanks!. I was looking for it. I am building an app for one computer/client and I dont want him to install two apps(jre an my custom app) - he will run just one setup file. :) – Michal - wereda-net Mar 11 '21 at 14:51
32

On OSX, they are moving away from Apple providing Java to Oracle providing Java, this also means that Java will not be installed on a fresh install of OSX. Oracle have provided a tool for packing up Java installations and they strongly recommend providing a jre as part of your installation. The advantage of this is that the customer will not have to install an additional package in order to run your application, and you can test your application against the correct Java runtime for you and ensure there are no incomptabilities before shipping. The disavantage is that building the installer is slighty more complex and your download size is larger.

Of course building installers get more complex if you want to provide your applications for Windows, Linux ectera and it would be alot simpler to just provide an executable jar but this is not the experience customers want. Customers do not expect to be able to download one application and run the same exe on windows, osx and linux. They are happy
to have different installers for each platform and also expect the installers to work in a different way.

This is how I do it:

OSX:Use AppBundler with bundled jre, put onto a Dmg with DMGCanvas. The user simply drags the application to their /Application folder.

Windows:Use Izpack with bundled jre wrapped with launch4j so installer can be run as an exe.

Linux:Use Izpack without bundled jre, as user linux users like to be in control of exactly what on their machine, but specify minimum version of Java allowed. Ideally I should create packages for the main Linux Package Managers such as rpm but Ive decided this is not worth the effort at the moment as Linux users are a small percentage of my client base.

Most of the installation is automated using Maven and Ant, so its not a big effort to build these different installers.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Are there any good resources on how to pack a jar within Izpack? What do you currently use for OSX, as AppBundler doesn't seem to be available anymore. – Genie Kort Sep 01 '17 at 12:12
  • @GenieKort I have struggled with Izpack documnetation, but havent looked at it for a long time.I use this fork of appbundler that is still active https://bitbucket.org/infinitekind/appbundler (what are you meant to do on OSX now ?) – Paul Taylor Sep 02 '17 at 10:02
  • How do you copy the jre as part of your build process? – Pablo Fernandez Apr 05 '18 at 07:46
9

We have the same situation with a commercial product in production. We've downloaded both windows and linux JREs of a specific version, and packed it with the application using IzPack.

"Our" JRE must be installed if one wants to install the app, and the app is being run by that JRE regardless of the system installed JREs or JDKs.

The downside is: installer is to be made for Windows or Linux and it is ~ 50 MB in size. The sizes of modules when unpacked are:

  • app itself ~ 5MB
  • libs ~10 MB
  • JRE ~90 MB

If I'd want to make it more "platform independent" I'd have to pack both JREs which would be ~100 MB for the installer. And it would still be valid for only two types of platforms (see Cthulhu' answer).

Note that we make the mostly remote installation's via SSH and that using compression options increases the package time by a factor of ~10 at least (from minutes to dozens of minutes).

Blackwood
  • 4,504
  • 16
  • 32
  • 41
linski
  • 5,046
  • 3
  • 22
  • 35
6

A rather new solution, provided by Oracle, is here:

Self contained packaging

It reads:

Self-contained application packages provide the following benefits:

  • Users install the application with an installer that is familiar to them and launch it in the usual way.

  • You control the version of the JRE used by the application.

  • Applications can be deployed on fresh systems with no requirement for the JRE to be installed.

But a drawback of this approach is:

Self-contained application packages can only be built using JDK 7 Update 6 or later.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 2
    Be aware, javapackager, at least up to version 1.8.0 172 of Java (when it was called javafxpackager) is buggy and badly designed. I've been workarounding bugs and limitations of it for a month now. – Pablo Fernandez Apr 05 '18 at 07:46
5

Look if you are planning to distribute this bundle (your application + JRE) then there is a utility install4J which is a powerful multi-platform Java installer builder that generates native installers and application launchers for Java applications.

kaysush
  • 4,797
  • 3
  • 27
  • 47
3

You can deploy it with java webstart which enables you to download a certain version of the jre if not present. I don't know if downgrading is possible, however. Java must be installed already, too.

atamanroman
  • 11,607
  • 7
  • 57
  • 81
2

You need platform-specific launchers that use a JRE that is placed next to the JAR file. Of course you can do this with batch files and shell scripts, but there are more professional solutions that create native launchers, such as install4j. (Disclaimer: my company develops install4j). A free solution is izpack.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
1

If it is a NetBeans platform application, NetBeans itself has the option to bundle additional resources and JRE .

Source: https://platform.netbeans.org/tutorials/nbm-nbi.html#bundling-jre

Jerin
  • 1,124
  • 9
  • 11
0

This is not directly possible. To make it possible, you have to download VM's for every platform. For windows, copy the JRE to a directory and the jar and call distributed JVM by shell scripts. But this is so hard and the solution described by Cthulhu is best i.e., check the version and point to the webpage.

However there are some you might check out.

JSmooth - http://jsmooth.sourceforge.net/

Jar2Exe - http://transfer2pc.weebly.com/1/post/2011/10/jar2exe-converter-11-gui.html

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91