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
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.
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:
dist
folderjre
folder to dist\jre-win-1.7.0_04
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.
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.
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.
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:
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).
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