I wanted to create a JavaFX app that can be distributed, but since I made a trial and error, I will summarize it here.
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
※macOS Mojave(10.14.6)But I was able to create it by the same procedure.
$ java --version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3)
OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Ubuntu-1ubuntu218.04.3, mixed mode, sharing)
$ jlink --version
11.0.4
At first I was thinking of using a JDK such as Amazon Corretto that ships with JavaFX. However, after reading the following article, I decided to use Openjdk-11 this time, JavaFX as a library, and generate a lightweight JRE at the time of distribution. Selecting a JDK in the OpenJFX era-or distributing applications in the OpenJFX era
In IntelliJ, create a new gradle project.
Create build.gradle by referring to the following source described in OpenJFX official website. https://github.com/openjfx/samples/blob/master/IDE/IntelliJ/Modular/Gradle/hellofx/build.gradle
In addition, since "Automatically import this project on changes in build script files" is checked in the IntelliJ settings, the module will be imported.
build.gradle
plugins {
id 'java'
id "org.openjfx.javafxplugin" version "0.0.8" //add to
}
group 'com.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
//add to
javafx {
version = "13"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
//Specify Main class when creating executable Jar
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
Main.java
import javafx.application.Application;
public class Main {
public static void main(String... args){
Application.launch(MyApplication.class);
}
}
MyApplication.java
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApplication extends Application {
public MyApplication(){
super();
}
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Hello");
primaryStage.setWidth(300);
primaryStage.setHeight(200);
primaryStage.show();
}
}
As a result of executing this, the following screen will appear.
At the console, run gradle build
.
If there is no problem, I think that an executable jar file will be created.
If you try to execute the jar file in this state ...
As you can see, I get angry that the javafx.application.Application class is missing. This is because there is no JavaFX module in jre of openjdk-11.0.4.
Now let's use jlink to create a lightweight jre to make this jar file work! This time, I referred to the following article. How to make a lightweight JRE for distribution
First, download jmods from Downloads on the following page. https://openjfx.io/
Extract the downloaded zip file and place it in an appropriate path.
(This time, it is placed in / usr / lib / jvm / javafx-13-openjfx / javafx-jmods-13
.)
This time, I downloaded jmods for Linux.
Next, use the ``` jdeps $ {jar file path}` `` command to check the modules required to execute this jar file.
This time, it seems good to use the following modules. · Java.lang-> java-base · Java.application-> javafx.base -Javafx.stage-> javafx.controls
Now that we know the modules we need, let's create a lightweight jre. I was able to create it by specifying the above module with the following jlink command.
$ jlink --compress=2 --module-path /usr/lib/jvm/java-11-openjdk-amd64/jmods:/usr/lib/jvm/javafx-13-openjfx/javafx-jmods-13 --add-modules java.base,javafx.base,javafx.controls --output jre
A jre directory has been created in the current path.
jre/bin/java
Binaries have also been created.
Now, let's execute the program using the jre created last.
$ ./jre/bin/java -jar build/libs/javafx-sample-1.0-SNAPSHOT.jar
I was able to execute it safely!
After that, put the JAR file and jre in a directory, create a shell or bat file for execution, or put them in an app package, and you're done. (The following is an example of creating a shell by putting it in a folder)
$ ls
javafx-sample-1.0-SNAPSHOT.jar jre start.sh
$ cat start.sh
#!/bin/sh
BASE=$(cd $(dirname $0); pwd)
$BASE/jre/bin/java -jar $BASE/javafx-sample-1.0-SNAPSHOT.jar
## sh start.Can be executed with sh
For the first time, I created a lightweight JRE using commands such as jlink and jdeps, but I think this method is easy when creating a JavaFX application that can be executed independently of the user's environment (jdk, etc.). The size of the jre directory this time is 368.3MB, so I wonder if it will be heavier if I can do more.
that's all. Thank you very much!
Recommended Posts