Luban College java source learning
Jpackage command usage scenarios
The usage scenario is to package the java desktop program. You can double-click to use the java program on windows/mac, and there is no requirement for whether there is jdk/jre in the system.
Here, let's first take a look at how the previous java desktop program worked:
jdk8 and before
At this time, the java client program is mainly divided into two parts, one is the executable jar package, and the other is the jdk/jre in the computer, and then through
java -jar XXXXX.jar to run the program.
Modularization of jdk9 and later
This is a dividing line of java modularity. In fact, I have never used jdk9 or jdk10. I tried jdk11 directly. On the basis of modularization, the project has one more file, moudle-info.java as shown in the figure below :
As shown in the figure above, there are mainly three keywords: requires, opens, and exports.
requires: If the current module wants to run, what dependencies are needed, which is equivalent to declaring the dependencies again on the basis of maven quoting the package
opens: Which packages in the current module can be reflected. For example, the controller to be processed is declared in fxml. Whenever fxml is loaded, a new controller instance will be created, so the package name of the opens controller is required to javafx.fxml
exports: Which packages of the current module can be referenced. Generally used when other modules depend on the current module
jdk9 and later packaging methods
In jdk9 and later, the packaging method can be not limited to jar packages, and these modules of jdk can be combined with jlink into a mirror.
As follows, I use gradle's jlink to package
The final result:
As shown above, the image folder will be generated in the build directory, and there will be bin, conf, lib in it. Then run Sunflower in the bin directory with cmd, as shown below: Remember this Sunflower, I want Let him bloom on the desktop of the majority of java programmers, haha
As you can see, at this time, in addition to the main body of the program, there is also a cmd window here, so inconvenient!!! At this time, jdk13 and later jpackage commands come in handy, just when I wrote this, jdk14 early access The version is released, so I directly experimented with jdk14
JDK14 jpackage command
After installing jdk14, you can use the jpackage -h command to see the instructions, as shown in the figure below
In fact, this is a layer of encapsulation of the mirror after the previous jlink has a mirror.
The packaging command I finally succeeded in experimenting is:
/Users/lixiang/soft/jdk-14.jdk/Contents/Home/bin/jpackage --runtime-image image --type dmg --name Sunflower --module red.lixiang.tools.desktopmain/com.platform.tools.desktop.DesktopMain
--runtime-image is followed by the folder, which is the image folder in our build directory
What kind of package is --type labeled? There are three options for app-image, dmg, and pkg in the mac system. I haven't tried it for windows. There should be an exe option
--name The name of the software, here is Sunflower
--moudle The name of the module, which is equivalent to the format of the previous Main function, replaced here with the module'package name'/Main function name
The results after running are as follows:
Finally, as shown above, you can see the generated dmg file, which can be used after installation on the mac