So, let's consider you want to make a JavaFX desktop application, here are the steps you should do to have an application:
Phase 1 - Install the necessary applications/frameworks
- Download JDK 8 (from here)- Download and install Netbeans for JDK 9 (from here) - it will work without the JDK9 installed
- Download JDK 9 from Early Access packages based on your architecture. They bundle JRE and JDK (from here).- Create a new project from NetBeans (pick a simple not-Maven application)
- go to project preferences and add make sure you add as a secondary JDK the JDK 9
- set the project to have both language level and JDK to be configured to Java 9/JDK9
Phase 2 - Build the application against Java 9/JDK 9
- write the application as following:
package javaapplication1;
public class Main {
public static void main(String[] args) {
System.out.println("Hello JDK9");
}
}
- add in the default package (in the root of src folder) the file: module-info.java and add this code inside:
module javaapplication1 {
requires java.desktop;
}
Build and run. It should run nicely.
Phase 3 - make a JDK distribution
- change directory to your project path:
cd
- jlink your Java distribution:
jlink --modulepath "C:\Program Files\Java\jdk-9\jmods" --modulepath "C:\Users\<YOUR_USERNAME>\Documents\NetBeansProjects\JavaApplication1\dist\JavaApplication1.jar" --addmods javaapplication1 javaapplication1.Main --output AppRuntime
By default the Java runtime which is required to run a Java desktop application is: 188 MB (on Windows x64 architecture).
The AppRuntime (it looks to me that it doesn't include my original application, but it excludes most of runtime for a JavaFX application is only 63.4)
- run your application using the minimalist distribution:
"appruntime/bin/java" -cp "dist\JavaApplication1.jar" javaapplication1.Main
Bonus:
If you want to really save space, you can compress your distribution)
jlink --modulepath "C:\Program Files\Java\jdk-9\jmods" --modulepath "C:\Users\<YOUR_USERNAME>\Documents\NetBeansProjects\JavaApplication1\dist\JavaApplication1.jar" --addmods javaapplication1 javaapplication1.Main --compress=2 --output AppRuntime
Now your Java distribution is less than 40 MB!
If you don't use JavaFX (for very simple applications, but just java.base), your distribution can slim down a lot, to be more precise, the Java distribution is around 22 MB as of latest build (with compression). This includes the Java.exe launcher Server environment.
Why is it important?
The most important reason I see this mode of making a minimalist distribution is for many deploy scenarios where installing of a runtime is a big NO-NO. For example in server environments, locked down Windows environments where is still allowed to run local applications and so on.
It is even more helpful that eventually you can put everything in a zip with no hard configurations.
No comments:
Post a Comment