Java8 and javafx8 package exe Win+Mac+Linux multi platform distribution, etc

Java8 and javafx8 package exe+Wind+Mac+Linux multi platform distribution, etc

I wrote a tutorial on javafx 17 packaging exe earlier. A classmate read it and said that the demo he wrote was that jdk8 packaging exe failed emm... In fact, it is easier to package jdk8 packaging exe without packaging dll operation.

When you think that using this article jar to package exe is suitable for jdk8 https://blog.csdn.net/weixin_44480167/article/details/121318205 , routine is not applicable!
You will find that nothing will be displayed after opening exe. Because java8 needs an additional jfxrt.jar package to run normally, you can execute cmd: xxx.exe > out.txt to display an error.

Next, I will demonstrate the jdk8 application of javafx to package exe. The routine is to package exe with packr. Packr can be packaged on multiple platforms such as Mac, Linux and window.

1, javafx application packaging jar

The following is a simple javafx application of mine, which adopts jdk8+Maven+jfoenix+controlsfx+fontawesomefx and introduces log logback

It's a little complicated, isn't it? Next, package it into jars. Just package it as ordinary jars, but pay attention to configuring the resource directory.
File "Proiect Structure" Artifacts in idea

Select the startup class:

Note that the following step is the key. You need to configure jfxrt.jar, click + Extracted Directory, and put C: \ program files \ Java \ jre1.8.0 of jre8_ 202 \ lib \ ext \ jfxrt.jar added. The key to jdk8 startup is jfxrt.jar



Click the packaged jar to display the following configuration, leave the class path blank (it is also blank by default), and click OK.
Then we build him:


Then java -jar runs it:

Log configurations have been generated normally:

Pay a little attention to the release method of the customized configuration file, that is, package the file into jar, then release it with:

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author lingkang
 * @date 2021/11/13
 */
public final class JConfig {

    private static JSONObject config = new JSONObject();
    private static File file;

    public static void init() {
        try {
            System.out.println(System.getenv("tmp"));
            System.out.println(getPath());
            file = new File(getPath() + "/config.json");
            String result = "";
            if (file == null || !file.exists()) {
                // It is generally used to release internal resources packaged with jar s
                InputStream inputStream = JConfig.class.getResourceAsStream("/config/default-config.json");
                BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
                String cc = null;
                while ((cc = bf.readLine()) != null) {
                    result += cc;
                }
                bf.close();
                inputStream.close();
                config = (JSONObject) JSONObject.parse(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Write the configuration file to the current jar directory
        FileUtil.writeUtf8String(config.toJSONString(), new File(getPath() + "/config.json"));
    }

    public static <T> T get(String key) {
        Object o = config.get(key);
        if (o != null) {
            return (T) config.get(key);
        }
        return null;
    }

    public static void set(String key, Object value) {
        set(key, value, false);
    }

    public static void set(String key, Object value, boolean isUpdateFile) {
        config.put(key, value);
        if (isUpdateFile) {
            FileUtil.writeUtf8String(config.toJSONString(), file);
        }
    }

    public static String getPath() {
        String path = JConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        if (System.getProperty("os.name").contains("dows")) {
            path = path.substring(1, path.length());
        }
        if (path.contains("jar")) {
            path = path.substring(0, path.lastIndexOf("."));
            return path.substring(0, path.lastIndexOf("/"));
        }
        return path.replace("target/classes/", "");
    }

}

2, Package exe distribution (note the jdk download address)

packr can be packaged and distributed on multiple platforms, so I don't count the title party. Here I only show win packaging.
Packr Download: https://github.com/libgdx/packr
Download packr-all-4.0.0.jar
Download openjdk. Do you think I'll go to the mirror station of Tsinghua University to download it? Wrong, the JDK downloaded from the mirror station of Tsinghua University is suitable for web applications, not GUI! Then you asked me why jdk17 can run, because I packed javafx independently. Openjdk of mirror mirror mirror station of Tsinghua university can't run javafx!
Download an openjdk17 and download the official jre of openjdk from here: https://adoptopenjdk.net/releases.html

https://adoptium.net/releases.html?variant=openjdk8

Finally point to GitHub: https://github.com/adoptium/temurin8-binaries/releases

Download openjdk8u JRE_ x64_ windows_ hotspot_ 8u312b07.zip and put them together. to write:

my-packr-config.json

{
    "platform": "windows64",
    "jdk": "OpenJDK8U-jre_x64_windows_hotspot_8u312b07.zip",
    "executable": "jdownloadApp",
    "classpath": [
        "jdownload.jar"
    ],
    "mainclass": "top.oneit.jdownload.JdownloadApplication",
    "vmargs": [
       "Xms32m","Xmx512m"
    ],
    "minimizejre": "soft",
    "output": "out-windows64"
}

function:

java -jar packr-all-4.0.0.jar my-packr-config.json
# perhaps
java -jar -Dfile.encoding=utf-8 packr-all-4.0.0.jar my-packr-config.json


Run exe

If nothing happens when you run exe, you can cmd run xxx.exe to view the errors output by the console and solve them according to the errors.

The above error is that the resources cannot be recognized after jar packaging. You can consider taking out fxml independently and calling fxml/xxx.fxml in the current directory when calling
I recommend using jdk11 + to develop javafx. In fact, jdk8 packages many holes in exe, unlike jdk11 + to separate the GUI.

Tags: Java javafx exe

Posted on Tue, 16 Nov 2021 20:29:10 -0500 by pit