简体   繁体   中英

How to create an executable file from a jar for mac os (using windows or linux) with a JRE included

Good evening everyone.

I am searching for many days a simple and non-deprecated answer to my question:

I've created a java program, on my computer (Windows). From that program I've created a runnable jar, which perfectly runs on my computer. Now, I want to convert that jar into a runnable file format for MAC OS, and include a JRE inside my app (so my client won't have to install java on each of his computer).

After some research, the only solutions that I've found are too old (unusable) or very complicated (and not enough documented).

I've heard about jar2app, but the part about the JRE that you have to include is not really clear. A lot of people also answer with launch4j, but it only creates Windows executable.

I have both Linux and Windows, but can't access to a MAC OS computer to develop anything (just test). I am using java 1.8.

Given below is an excerpt from this article and I hope, it should work for Java-8 too:

Since this example defines the runtime sub-element using JAVA_HOME , make sure it is configured correctly for your environment. For example, in your .bashrc file , define JAVA_HOME as follows:

export JAVA_HOME=/usr/libexec/java_home

Use the following steps to modify the build.xml file at the top of the project directory:

  1. Specify an environment property, named env:

    <property environment="env" />

  2. In the target that creates the bundle, specify the location of the JRE on your system, using the env property:

    <runtime dir="${env.JAVA_HOME}" />

The resulting build.xml file should look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project name="ButtonDemo" default="default" basedir=".">
    <import file="nbproject/build-impl.xml"/>

<property environment="env" />

<taskdef name="bundleapp"
         classname="com.oracle.appbundler.AppBundlerTask"
         classpath="lib/appbundler-1.0ea.jar" />

<target name="bundle-buttonDemo">
    <bundleapp outputdirectory="dist"
        name="ButtonDemo"
        displayname="Button Demo"
        identifier="components.ButtonDemo"
        mainclassname="components.ButtonDemo">
        <runtime dir="${env.JAVA_HOME}" />
        <classpath file="dist/ButtonDemo.jar" />
    </bundleapp>
</target>

</project>

Create a fresh version of ButtonDemo.app , using the ant bundle-buttonDemo command. The resulting version includes the JRE in the app package. You can confirm this by examining the Contents/PlugIns directory inside of the app package.

Consider using this Runtime Plugin for non-modular apps. Or if it's modular, there is a similar plugin . The github page is active, and issues are addressed promptly in my experience. It builds for Windows, Linux and OS X.

Even though it uses the recent Java feature of jpackage , you should still be able to compile your code in an earlier JDK version while using jpackage from a newer JDK version.

I recently updated a project to use it, which you are welcome to look at (give feedback if you do). There are also other examples from the plugin's site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM