I started developing applications in Java JavaFx a little over a month ago.
I was able to make a couple of beautiful animated applications. When compiling to a jar-file, I ran into the following problems:
Java17 doesn't run with jre which is for normal people, https://www.java.com/en/download/ , this begs the first question, why can't I develop on newer versions of java jdk without pain, why should I go down to version 8 and lose a lot of features
Also my javafx jars didn't want to run at all without any console messages (javafx unnamed .... @729347c ) and wouldn't run on double click. There were also messages like No runtime for favafx blah blah blah. I solved it by installing liberica jdk https://bell-sw.com/pages/downloads/#/java-17-lts. Now I can successfully compile jar files and run them by double clicking
I read right there, in a bunch of topics, that Oracle now means that I have to deliver to the clients the required version of jre (Jre17), the question is how can I do this. Newer versions of Lauch4j removed the ability to package jar along with jre. But I don't like Lauch4j either.
Later I learned about such thing as native-image in GraalVm. I tried compiling a jar file with javafx and it didn't work for me in any way. The program just crashed. And I found on the BellSoft website the package I needed https://bell-sw.com/pages/downloads/native-image-kit/#/nik-22-17, which has a configured graalvm. Thinking that these gods solved all my problems again, I compiled my jar file into an exe (yes, I changed the jdk in the environment paths). And Now I am getting Segment Fault message. But this is already better, at least it starts, although it crashes immediately after launch. Inside, by the way, the code of the usual default javafx program from Inteliji with the Hello javafx button
In general, I painted all the problems that I encountered. And I have 2 questions
- How can I distribute my javafx programs with my own jre like big companies do?
- How to compile javafx program to native-image?
My environment:
- Windows 10
- Liberica Native Image Kit 22.3.0 2 (java17 jdk)
- Latest version of Inteliji
I will also clarify that I do not want to force clients to download anything (except perhaps https://www.java.com/en/download/, but no more), I want a completely native program
Sorry if it's messy
everything I tried and did is described above
CodePudding user response:
Here are two ways you can go about making your applications packed for distribution:
- Using JLink:
One way to distribute your JavaFX applications with your own JRE is to use a tool called "jlink" that is included in the Java Development Kit (JDK). Jlink allows you to create a custom runtime image that contains only the modules and dependencies required by your application. This can help you create a smaller, more efficient runtime for your application that does not require users to install a full JRE.
To create a custom runtime image with jlink, you will need to specify the modules and dependencies required by your application. You can do this by creating a "module-info.java" file in your application's source code that declares the dependencies your application requires. You can then use the jlink tool to create a custom runtime image that includes these dependencies.
Once you have created a custom runtime image, you can package it along with your application in a self-contained installer or bundle. This will allow users to install and run your application without having to install a separate JRE.
- Using the Native Image Tool
To compile a JavaFX program to native-image, you will need to use the GraalVM Native Image tool. This tool allows you to compile your Java application into a native executable that can be run on a specific platform without requiring a JRE.
To use the Native Image tool, you will need to install GraalVM and configure it as your JDK in your development environment. You can then use the native-image command to compile your JavaFX application into a native executable.
It is worth noting that the Native Image tool has some limitations and may not support all features of the Java runtime. In particular, it may not support certain reflective or dynamic features of the language, such as reflection or dynamic class loading. You may need to modify your application or use special flags and configuration options to get the Native Image tool to work with your application.
CodePudding user response:
I understand your frustration because properly packaging JavaFX applications is made mored complicated than it should be. There are a lot of tools that you have to combine which also leave much room for errors. The main problem is that the official OpenJFX Maven plugin (https://github.com/openjfx/javafx-maven-plugin) still doesn't have a packaging option. It has a Jlink goal but that does not help much in practice because it requires a fully modularised application which I have not yet seen in the wild for any serious project because many important libraries are not yet modular (and probably never will be).
Together with Dirk I have therefore created a demo project which combines jdeps, jlink and jpackage in a way that it can also handle non-modular projects and build an application and installer for all desktop platforms. Maybe you want to have a look at this repo: https://github.com/dlemmermann/JPackageScriptFX
You could also use Gluons GluonFX plugin (https://github.com/gluonhq/gluonfx-maven-plugin) which does contain a packaging option but that internally uses GraalVM native-image which requires some extra care to get it going and I personally never felt the need for this on desktop. It's the best option though if you also want to go mobile.