Java 16 JPackage 打包可执行 exe

博文目录

文章目录

  • Maven 工程结构
    • JDK 17
    • pom.xml
    • com.mrathena.Gui
    • 打包 jar (idea 自带功能)
  • Java 16 JPackage
    • 部分参数说明
    • WIX v3
  • 打包


Maven 工程结构

Java 16 JPackage 打包可执行 exe_第1张图片

JDK 17

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.mrathenagroupId>
    <artifactId>demoartifactId>
    <version>0.0.1version>

    <dependencies>
        <dependency>
            <groupId>com.formdevgroupId>
            <artifactId>flatlafartifactId>
            <version>3.0version>
        dependency>
    dependencies>

    <properties>
        <maven.compiler.source>17maven.compiler.source>
        <maven.compiler.target>17maven.compiler.target>
    properties>

project>

com.mrathena.Gui

package com.mrathena;

import com.formdev.flatlaf.FlatDarkLaf;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Objects;

public class Gui {
    private JPanel root;
    public static void main(String[] args) {
        FlatDarkLaf.setup();
        JFrame frame = new JFrame("Gui");
        ImageIcon icon = new ImageIcon(Objects.requireNonNull(Gui.class.getClassLoader().getResource("icon.png")));
        frame.setIconImage(icon.getImage());
        frame.setContentPane(new Gui().root);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 450);
        frame.setLocationRelativeTo(null); // 居中
        frame.setVisible(true);
    }
}

打包 jar (idea 自带功能)

demo.jar, 大小 862KB, 确保执行 java -jar demo.jar 能正常运行, 打开窗体

Java 16 JPackage 打包可执行 exe_第2张图片

Java 16 JPackage

jpackage -h
C:\mrathena\develop\workspace\idea\demo\out\artifacts\demo_jar>jpackage -h
Usage: jpackage 

Sample usages:
--------------
    Generate an application package suitable for the host system:
        For a modular application:
            jpackage -n name -p modulePath -m moduleName/className
        For a non-modular application:
            jpackage -i inputDir -n name \
                --main-class className --main-jar myJar.jar
        From a pre-built application image:
            jpackage -n name --app-image appImageDir
    Generate an application image:
        For a modular application:
            jpackage --type app-image -n name -p modulePath \
                -m moduleName/className
        For a non-modular application:
            jpackage --type app-image -i inputDir -n name \
                --main-class className --main-jar myJar.jar
        To provide your own options to jlink, run jlink separately:
            jlink --output appRuntimeImage -p modulePath \
                --add-modules moduleName \
                --no-header-files [...]
            jpackage --type app-image -n name \
                -m moduleName/className --runtime-image appRuntimeImage
    Generate a Java runtime package:
        jpackage -n name --runtime-image 

Generic Options:
  @
          Read options and/or mode from a file
          This option can be used multiple times.
  --type -t 
          The type of package to create
          Valid values are: {"app-image", "exe", "msi"}
          If this option is not specified a platform dependent
          default type will be created.
  --app-version 
          Version of the application and/or package
  --copyright 
          Copyright for the application
  --description 
          Description of the application
  --help -h
          Print the usage text with a list and description of each valid
          option for the current platform to the output stream, and exit
  --icon 
          Path of the icon of the application package
          (absolute path or relative to the current directory)
  --name -n 
          Name of the application and/or package
  --dest -d 
          Path where generated output file is placed
          (absolute path or relative to the current directory)
          Defaults to the current working directory.
  --temp 
          Path of a new or empty directory used to create temporary files
          (absolute path or relative to the current directory)
          If specified, the temp dir will not be removed upon the task
          completion and must be removed manually.
          If not specified, a temporary directory will be created and
          removed upon the task completion.
  --vendor 
          Vendor of the application
  --verbose
          Enables verbose output
  --version
          Print the product version to the output stream and exit.

Options for creating the runtime image:
  --add-modules [,...]
          A comma (",") separated list of modules to add
          This module list, along with the main module (if specified)
          will be passed to jlink as the --add-module argument.
          If not specified, either just the main module (if --module is
          specified), or the default set of modules (if --main-jar is
          specified) are used.
          This option can be used multiple times.
  --module-path -p ...
          A ; separated list of paths
          Each path is either a directory of modules or the path to a
          modular jar.
          (Each path is absolute or relative to the current directory.)
          This option can be used multiple times.
  --jlink-options 
          A space separated list of options to pass to jlink
          If not specified, defaults to "--strip-native-commands
          --strip-debug --no-man-pages --no-header-files".
          This option can be used multiple times.
  --runtime-image 
          Path of the predefined runtime image that will be copied into
          the application image
          (absolute path or relative to the current directory)
          If --runtime-image is not specified, jpackage will run jlink to
          create the runtime image using options:
          --strip-debug, --no-header-files, --no-man-pages, and
          --strip-native-commands.

Options for creating the application image:
  --input -i 
          Path of the input directory that contains the files to be packaged
          (absolute path or relative to the current directory)
          All files in the input directory will be packaged into the
          application image.

Options for creating the application launcher(s):
  --add-launcher =
          Name of launcher, and a path to a Properties file that contains
          a list of key, value pairs
          (absolute path or relative to the current directory)
          The keys "module", "main-jar", "main-class",
          "arguments", "java-options", "app-version", "icon", and
          "win-console" can be used.
          These options are added to, or used to overwrite, the original
          command line options to build an additional alternative launcher.
          The main application launcher will be built from the command line
          options. Additional alternative launchers can be built using
          this option, and this option can be used multiple times to
          build multiple additional launchers.
  --arguments 
Command line arguments to pass to the main class if no command line arguments are given to the launcher This option can be used multiple times. --java-options Options to pass to the Java runtime This option can be used multiple times. --main-class Qualified name of the application main class to execute This option can only be used if --main-jar is specified. --main-jar
The main JAR of the application; containing the main class (specified as a path relative to the input path) Either --module or --main-jar option can be specified but not both. --module -m [/
] The main module (and optionally main class) of the application This module must be located on the module path. When this option is specified, the main module will be linked in the Java runtime image. Either --module or --main-jar option can be specified but not both. 用来创建应用程序启动程序的与平台相关的选项: --win-console 为应用程序创建控制台启动程序,应当为 需要控制台交互的应用程序指定 Options for creating the application package: --about-url URL of the application's home page --app-image Location of the predefined application image that is used to build an installable package (absolute path or relative to the current directory) --file-associations Path to a Properties file that contains list of key, value pairs (absolute path or relative to the current directory) The keys "extension", "mime-type", "icon", and "description" can be used to describe the association. This option can be used multiple times. --install-dir 默认安装位置下面的相对子路径 --license-file Path to the license file (absolute path or relative to the current directory) --resource-dir Path to override jpackage resources Icons, template files, and other resources of jpackage can be over-ridden by adding replacement resources to this directory. (absolute path or relative to the current directory) --runtime-image Path of the predefined runtime image to install (absolute path or relative to the current directory) Option is required when creating a runtime package. Platform dependent options for creating the application package: --win-dir-chooser Adds a dialog to enable the user to choose a directory in which the product is installed. --win-help-url URL where user can obtain further information or technical support --win-menu Request to add a Start menu shortcut for this application --win-menu-group Start Menu group this application is placed in --win-per-user-install Request to perform an install on a per-user basis --win-shortcut Request to add desktop shortcut for this application --win-shortcut-prompt Adds a dialog to enable the user to choose if shortcuts will be created by installer. --win-update-url URL of available application update information --win-upgrade-uuid UUID associated with upgrades for this package

部分参数说明

jpackage - 用于打包自包含 Java 应用程序的工具。由Oracle官方文档翻译

参数 说明
application package suitable 安装程序, 而不是直接运行程序
application image 运行程序, 其实就是安装程序安装完成后生成的内容
- -
--input -i 指定打包目录, 该目录下的所有内容都会被打包, 在其他参数里引用该目录下的文件可使用相对路径, 打包前可以把jar/ioc等文件拷贝到这个目录
--dest -d 生成物的输出路径, 默认值就是当前cmd的工作路径, 当type选app-image时, 切记输出路径不能与–input指定的目录在同一个路径, 因为不显示指定–dest时, 默认的输出路径就是当前路径, 即–input指定的路径, 而–input指定目录中的所有内容都会被打包, 这种情况下, 生成程序会陷入死循环, 生成一些无限嵌套的文件夹, 而且无法删除
--name -n 应用名称
--type -t 要生成的包的类型, 可选值是 {“app-image”, “exe”, “msi”}, exe和msi都是安装包, app-image是可直接运行的程序
--app-version 应用版本号, 必须是 1.0, 1.3.2, 类似的格式, 数值在 [0-255], 不能随意指定任意字符串
--icon 生成的包的图标, 如果生成的是安装程序, 则安装程序本身使用该图标, 安装后的可执行exe并没有使用该图标, 如果生成的直接是可执行程序, 则该可执行程序使用该图标
--vendor 应用作者
--copyright 应用版权
--description 应用描述
``
``

WIX v3

在这里插入图片描述

使用 jpackage 需要安装 WIX v3 及更高版本, 官网, GitHub

安装完貌似不需要配置环境变量即可直接使用

打包

  • Modular Java Project (非 Maven / Gradle) 打包
  • Modular Java Maven Project 打包
  • Non-Modular Java Project 打包

参考如下文章内容

Windows Java JavaFX IntelliJ IDEA 开发环境搭建 创建工程 编译运行 打包分发 自定义运行时

你可能感兴趣的:(java,java)