I want to bundle a JRE inside my executable JAR, so that the exe can run on any system.
I have tried Launch4j, but that approach requires me to ship both the JRE and the exe. As per my requirements, I should not use an installer to ship, nor extract/install the JRE on the client machine.
How do I put a JRE inside a JAR and make use of it?
If you do not have Java installed, and the PATH variable is not set correctly, attempts to run a JAR file on Windows or Ubuntu will result in a 'Java not recognized' error. To run a JAR file, you must install the Java JDK or JRE on your computer.
To run a jar file you only need java.exe(windows). JDK is the development kit for Java and JRE is the runtime. JDK contains JRE.
Bundling a JRE, basically means that your application includes its own JRE, that gets installed, along with your application; typically within the same folder (Windows) or within the app bundle (Mac).
If you want to run any Java program on your Windows PC, you won't be able to do it without installing the Java Development Kit (JDK for short). The JDK also contains the Java Runtime Environment (or JRE) which is the core of a Java program.
You cannot put a JRE inside a JAR file. (Well you can ... but it won't help.)
What you need to do is build and distribute an installer. The recommended way to create an installer is to use a commercial or open-source installer generator. (Google will help you find one.)
It is also possible to do embed a JRE in a ZIP file, as described here:
The ZIP file contains the JRE and the application JAR, and other files that it needs. The user has to unzip the ZIP file to install the application.
.. but it's copying total JRE in client system of almost 100 MB. Is it possible to make JRE light weight?
Not really. The most (legal) light-weight way to distribute a JRE is to distribute an Oracle Java installer. Beware that the Java Binary License forbids the distribution of a cut-down JRE. If you want to go down that path talk to a lawyer first!!
Distributing Java apps with an embedded JRE is arguably a bad thing:
From Java 9 onwards there is a better solution than inluding a standard JRE in your installer. Use the new jlink
utility to create a tailored JRE for your application; see https://www.baeldung.com/jlink for a tutorial, and the Oracle Jlink manual entry.
Note that jlink
addresses the concerns with embedded JREs that we mentioned above. But it clearly makes it your responsibility1 to provide JVM security patches to your users in the form of new jlink'd distributables.
1 - In reality, it has always been an application supplier / vendor's responsibility to advise and assist customers in upgrading embedded JREs. Unfortunately, many vendors neglected this, leading to customers running applications with known Java vulnerabilities, and Sun/Oracle getting the blame ... unfairly ... for the vendor's failings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With