Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deliver my Java application with a particular JRE?

Tags:

java

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!

like image 567
salocinx Avatar asked Oct 24 '12 08:10

salocinx


People also ask

Can Java application run without JRE?

You cannot do without JRE.

How do you package an application in Java?

The recommended way to package Java applications is to use a collection of Ant tasks ( ant-javafx. jar ), which are provided with the JRE. NetBeans IDE uses these Ant tasks to package JavaFX and Java SE projects. Embedded packaging support in NetBeans IDE covers most of the typical use cases.


2 Answers

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.

like image 52
Dbominable Avatar answered Sep 30 '22 16:09

Dbominable


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.

like image 45
Paul Taylor Avatar answered Sep 30 '22 16:09

Paul Taylor