How do I bundle a JRE in my JAR, so that it can run on systems without Java?

Viewed 18922

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?

3 Answers

It did work for me with launch4j

first create an app folder then open launch4j and type in the jar file, output file (should be in the app folder), and the icon (please have it in app folder). then go to the jre part and type jre-{version ur using} in the bundled jre paths then put option only use private jdk runtimes then put the min version to ur java version, then go the jdk installation and copy the bin and lib folder then create a jre-{version ur using} folder then paste these in. Then compile it into exe!

Late better than never. I used maven-shade-plugin in the pom.xml as follows:

        <plugin>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.4.3</version>
          <configuration>
              <transformers>
                  <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                  <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>${mainClass}</mainClass>
                  </transformer>
              </transformers>
              <!-- If false the shaded artifact takes the normal artifact name, such as,  "core-tools-1.0-SNAPSHOT.jar"-->
              <!-- Meanwhile, another non-shaded version takes the prefix Original, such as  "Original-core-tools-1.0-SNAPSHOT.jar"-->
            <shadedArtifactAttached>false</shadedArtifactAttached>
              <!-- Exclude signed Manifests from the UberJar -->
              <filters>
                  <filter>
                      <artifact>*:*</artifact>
                      <excludes>
                          <exclude>META-INF/*.SF</exclude>
                          <exclude>META-INF/*.DSA</exclude>
                          <exclude>META-INF/*.RSA</exclude>
                      </excludes>
                  </filter>
                  <filter>
                      <artifact>*:*</artifact>
                      <includes>
                          <include>sun/misc/**</include>
                      </includes>
                  </filter>
              </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Related