简体   繁体   中英

Packaging Jar file for Mac and Windows without JRE bundled

I built in IntelliJ IDEA my javaFX application as jar file. Using " Project Structure >> Artifacts >> + JavaFX Application " I can build .app, .dmg, .pkg, .exe with JRE included. But I need build same files without JRE bundled. Sure, I can use JAR, but I want to make my own icon and maybe installer.

I also tried to create .app folder from jar to execute it manually " java -jar myJarName.jar " , but if I have several JDK version - it always uses the latest (JRE 11) which not included javaFX library and my .app doesn't work. But if I run the same JAR with Jar Launcher.app it works perfectly. Somehow it chooses the right jre version. ExcelsiorJet, install4j and similar apps work well, but my project is OpenSource and I can not pay 3000$ for this.

The question is - how can I build MacOs/Windows native launcher app/dmg/pkg/exe without JRE bundled for users who already have JRE installed? Can I use IntelliJ IDEA to build this a way as I built with jre bundled?

Okay, looks like I found some crooked way to do this all.

Windows solution:

To bundle on Windows it's easy to use launch4j (windows only). It's free and there is no problem to create .exe with no Jre bundled.

MacOS solution:

For MacOS it's a bit harder:

  1. Create myApplication.app folder and design it's structure

    在此输入图像描述

  2. Write launcher bash script: In my case I should detect which versions of Jre installed and choose any between java 1.8 and 10

I don't know bash script language and I believe I write it unoptimized way. I would be happy if anyone correct me. Anyway it works the way as I wanted:

#!/bin/sh

# set the working directory
DIR=$(cd "$(dirname "$0")"; pwd) 

# extract first fit java version installed
jre_path=$(/usr/libexec/java_home -V 2>&1 |
while IFS= read -r line
do
    if [[ "$jre_found" ==  "true" ]]; then
        break
    fi
    version=$(echo $line | cut -d ' ' -f 1|sed 's/^ *//;s/ *$//' | cut -d ' ' -f 1 | sed 's/^ *//;s/ *$//')
    major=$(echo $version | cut -d. -f1)
    minor=$(echo $version | cut -d. -f2)
    array=(${line// /}) 
    array_size=${#array[@]}
    let "last_index=array_size-1"
    path=${array[ $last_index ]}

    if [[ $major == 1 ]]; then
        if [[ $minor -gt 7 && $minor -lt 11 ]]; then
            echo $path
            jre_found="true"
        fi
    elif [[ $major -gt 7 &&  $major -lt 11 ]]; then
        echo $path
        jre_found="true"
    fi
done)

# execute our jar file
$jre_path/bin/java -jar "$DIR"/myApp.jar

And now everything should be working from double click on myApplication.app .

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