Unlock new Java package posture

In normal times we'll pack it labeled Jar, then when running on other platforms need to install jre to support the operation.

So in fact, Java platform can be packaged into a native generic type, for example:

  • exe
  • installer
  • image
  • msi
  • dmg
  • rpm
  • deb

Of course I have access to a lot of blog and its official website information.

To see the most is exe4j, but saw some comments that can not run after the package, but after I downloaded exe4j is not running. So rule out this option.

In addition to exe4j it there, install4j, jwrapper and other tools, but they are charging, it is also excluded.

There Java9 of jaotearlier versions of the java14 jpackage, java8 ofjavapackager

In the review of the blog was mentioned in the java9 jaot, download java9, but did not find jaot, taking into account the common version 8, it is also ruled out jaot.

So the only java14 jpackageand java8 of javapackager(without any tailoring done to the jre), both of which I have had practice, the effect is almost always break out almost the size of the package.

Ultimately chose Java8 that comes with javapackagerthis tool. (Unfortunately, however, can not display the contents of the console, it is recommended to use the log file to replace the .java14 jpackagecan specify whether to display the console)

Now we have to look at how the jar package packaged into native form of the package.

Ready content

  • Setup Compiler INNO (Java8 support version 6 or less, do not download more than 6 and 6 version, otherwise it can not be packaged successfully)

  • the WiX (packaged into a msi must download, no download javapackagerwill prompt a lack wix)

  • java8 environment

  • Jar program example (As used herein, the jar as an example, to facilitate the exchange of learning)

    Program Description

    After starting the program, created in c drive called 我是测试文件.txtfile, write the word "sleep * seconds" will file every second until the end of the program to run after the 10s, at the end there will be written the word success. (If not generated after the start of the file, locate the Task Manager to end the process, and then run the program as an administrator.)

Let's start with the first pack it!

Prior to this, assume that you have read and prepare content completed.

  1. Download the package into your jar folder, and openpowershell

  2. Input:

    javapackager -deploy -native image  -outdir cs -outfile cs -srcfiles pack.jar -appclass cn.qs.CS -name CS
    复制代码
  3. You will find a call in the same directory csfolder, enter cs, then into the bundles folder. Find cs.exe, run by an administrator.

  4. Into the C drive will be able to find the program generated 我是测试文件.txtfiles

Thus, a simple package end.

Now we look at what the meaning of the above command.

javapackager---java8自带的打包程序

-deploy---用来构建目标机器的发行版本,简单说就是打包成exe或者其他平台的包,如果不带任何参数,会生成一个基本的应用程序,不建议不带任何参数

-native image---为jar创建磁盘镜像(可以将image替换为其他的类型,例如exe,msi,deb,rpm)

-outdir cs---输出目录。

-outfile cs----输出文件(不要带后缀,比如cs.exe,就写cs就行了)。

-srcfiles pack.jar---要打包的jar文件。

-appclass cn.qs.CS---jar文件的主类的全限定名。(注意是全限定名)

-name CS---启动之后的应用名称
复制代码

We will attach more parameters in the last article. You can also enter javapackagerto view the help, or go to the official website View

Do some customizing it

aims

  1. Modify the application icon
  2. Modify the application instructions (according to the official website operation, specified but invalid, it can be ignored)
  3. Modify the application vendor (vendor)

Note: Because it is a console application, so the title, width and height specified, have no meaning. So we are here to modify these two properties on it.

We start customizing it

Note: in the form of image tested, description (Description) and description (Vendor) is invalid, and in exe msi forms are possible

  1. In the same directory to download the jar, just delete the generated cs folder.

  2. Re-execute the command:

    javapackager -deploy -native exe  -outdir cs -outfile cs -srcfiles pack.jar -appclass cn.qs.CS -name CS -description "测试的描述" -vendor "测试厂商" -Bicon=".\cs.ico"
    复制代码
  3. Now we define success. (Free to look at, is not our custom icons, descriptions, vendor), then more about custom please refer to the official website (since these parameters defined in the bundle use the time to pay attention, -B add the parameter name = value For example, when using the icon it is -Bicon = "path")

bundle Parameter Description

In addition to the above parameters packaged bundle it, bubdle parameters in the bundle are some common parameters and the windows.

General parameters

  1. appVersion = version version of the application, such as: -BappVersion = 1.0.1
  2. classPath = path-based directory path relative to the application package (not the default settings.)
  3. icon = path path application icon, for example: -Bicon = ". \ cs.ico"
  4. identifier for the default values ​​for the other platform-specific.
  5. jvm jvmOptions = option to specify parameters, such as: -BjvmOptions = -Xmx128m -BjvmOptions = -Xms128m
  6. jvmProperties = property = value when the program is running, this parameter will be passed to the jvm can be obtained by System.getProperty (), java -Dproperty can be used, you need to specify parameter names and values, if you want to pass multiple please reference: -BjvmProperties = apiUserName = example -BjvmProperties = apiKey = abcdef1234567890
  7. mainJar = filename contains the name of the main class of the jar file, the file name is usually obtained from mainfest jar file, if you use javapackager command, do not specify this value
  8. preferencesID = node jvm options for checking the user may cover, designated as a runtime option -Dapp.preferences.idparameter passed to the application, the required parameters and userJVMOptions used together.
  9. position path Jdk runtime = jre or, if using the default jre not need to provide. For example: -Bruntime =
  10. = userJvmOptions the Option = value of any options that can be covered by a user jvm java command options used are valid, to pass more than one, please refer to: -BuserJvmOptions = -Xmx = 128m -BuserJvmOptions = -Xms = 128m

windows

  1. = Copyright String version string application, for example: 2002-2020 Jetbrains sro

  2. = LICENSEFILE path EULA position (EULA), e.g. -BlicenseFile = COPYING

  3. menuHit=boolean

    Marking whether to install a shortcut on the Start menu or Start screen, set to true to install a shortcut, default is true for example: -BmenuHint = true

  4. shortcutHint = boolean marks whether to install the desktop shortcut icon, set to true to install, the default is false. For example: -BshortcutHint = true

  5. systemWide = boolean marks the application is installed in the Programs Files or in UserData, set to true in the Programs Files installed, default is false

  6. win.menuGroup = group menuGroup only take effect when menuHint is true, menuHint to false This parameter is ignored.

  7. vendor = value also is the author, can be individuals, organizations, corporations and so on. For exe, or registry metadata.

Parameter Description

用法: javapackager -command [-options]

其中 command 为以下项之一:
  -createjar
          打包程序根据其他参数生成 jar 档案。
  -deploy
          打包程序根据其他参数生成 jnlp 和 html
          文件。
  -createbss
          将 css 文件转换为二进制形式
  -signJar
          使用提供的证书为 jar 文件签名。
  -makeall
          将 compilation, createjar 和 deploy 步骤作为一个调用执行,
          在其中预定义大多数参数。源必须位于 "src"
          文件夹中, 生成的文件 (jar, jnlp, html) 将放入 "dist"
          文件夹中。此命令只能以最简单方式配置, 并且
          尽可能自动进行。

createjar 命令的选项包括:
  -appclass <application class>
          要执行的应用程序类的限定名称。
  -preloader <preloader class>
          要执行的预加载器类的限定名称。
  -paramfile <file>
          包含默认命名应用程序参数的属性文件。
  -argument arg
          JNLP 文件的 <fx:argument> 元素中要放入的未命名
          参数。
  -classpath <files>
          相关 jar 文件名列表。
  -manifestAttrs <manifest attributes>
          附加清单属性列表。语法: "name1=value1,
          name2=value2,name3=value3"。
  -noembedlauncher
          如果存在, 打包程序将不会向 jarfile 添加 JavaFX
          启动程序类。
  -nocss2bin
          在复制到 jar 之前, 打包程序不会将 CSS 文件转换为
          二进制形式。
  -runtimeversion <version>
          所需 JavaFX 运行时的版本。
  -outdir <dir>
          要将输出文件生成到的目录的名称。
  -outfile <filename>
          生成的文件的名称 (不带扩展名)。
  -srcdir <dir>
          待打包文件的基目录。
  -srcfiles <files>
          srcdir 中的文件的列表。如果省略, 将对 srcdir (在
          这种情况下是必需的参数) 中的所有文件进行打包。

deploy 命令的选项包括:
  -title <title>
          应用程序的标题。
  -vendor <vendor>
          应用程序的供应商。
  -description <description>
          应用程序的说明。
  -appclass <application class>
          要执行的应用程序类的限定名称。
  -preloader <preloader class>
          要执行的预加载器类的限定名称。
  -paramfile <file>
          包含默认命名应用程序参数的属性文件。
  -htmlparamfile <file>
          包含所生成小应用程序参数的属性文件。
  -width <width>
          应用程序的宽度。
  -height <height>
          应用程序的高度。
  -native <type>
          生成自包含的应用程序包 (如果可能)。
          如果指定了类型, 则只创建此类型的包。
          所支持类型的列表包括: installer, image, exe, msi, dmg, rpm 和 deb。
  -name <name>
          应用程序的名称。
  -embedjnlp
          如果存在, 会将 jnlp 文件嵌入 html 文档中。
  -embedCertificates
          如果存在, 会将证书嵌入 jnlp 文件中。
  -allpermissions
          如果存在, 应用程序将需要 jnlp 文件中的所有
          安全权限。
  -updatemode <updatemode>
          设置 jnlp 文件的更新模式。
  -isExtension
          如果存在, srcfile 将被视为扩展。
  -callbacks
          在生成的 HTML 中指定用户回调方法。格式为
          "name1:value1,name2:value2,..."
  -templateInFilename
          html 模板文件的名称。占位符格式为
          #XXXX.YYYY(APPID)#
  -templateOutFilename
          要将已填充的模板写入到的 html 文件的名称。
  -templateId
          用于模板处理的应用程序的应用程序 ID。
  -argument arg
          JNLP 文件的 <fx:argument> 元素中要放入的未命名
          参数。
  -outdir <dir>
          要将输出文件生成到的目录的名称。
  -outfile <filename>
          生成的文件的名称 (不带扩展名)。
  -srcdir <dir>
          待打包文件的基目录。
  -srcfiles <files>
          srcdir 中的文件的列表。如果省略, 将对 srcdir (在
          这种情况下是必需的参数) 中的所有文件进行打包。

createbss 命令的选项包括:
  -outdir <dir>
          要将输出文件生成到的目录的名称。
  -srcdir <dir>
          待打包文件的基目录。
  -srcfiles <files>
          srcdir 中的文件的列表。如果省略, 将对 srcdir (在
          这种情况下是必需的参数) 中的所有文件进行打包。

signJar 命令的选项包括:
  -keyStore <file>
          密钥库文件名。
  -alias
          密钥的别名。
  -storePass
          用于检查密钥库完整性或对密钥库取消锁定的口令。
  -keyPass
          用于恢复密钥的口令。
  -storeType
          密钥库类型, 默认值为 "jks"。
  -outdir <dir>
          要将输出文件生成到的目录的名称。
  -srcdir <dir>
          待签名文件的基目录。
  -srcfiles <files>
          srcdir 中的文件的列表。如果省略, 将对 srcdir (在
          这种情况下是必需的参数) 中的所有文件进行打包。

makeAll 命令的选项包括:
  -appclass <application class>
          要执行的应用程序类的限定名称。
  -preloader <preloader class>
          要执行的预加载器类的限定名称。
  -classpath <files>
          相关 jar 文件名列表。
  -name <name>
          应用程序的名称。
  -width <width>
          应用程序的宽度。
  -height <height>
          应用程序的高度。
  -v      启用详细输出。

示例用法:
--------------
javapackager -createjar -appclass package.ClassName
  -srcdir classes -outdir out -outfile outjar -v
          将类目录的内容打包到 outjar.jar,
          将应用程序类设置为 package.ClassName。
javapackager -deploy -outdir outdir -outfile outfile -width 34 -height 43
  -name AppName -appclass package.ClassName -v -srcdir compiled
          在由 package.ClassName 类启动且大小为 34x43 的
          应用程序 AppName 的 outdir 中生成
          outfile.jnlp 和对应的 outfile.html 文件。
javapackager -makeall -appclass brickbreaker.Main -name BrickBreaker
  -width 600 -height 600
          此命令执行包含编译在内的所有打包工作:
          compile, createjar 和 deploy。

复制代码

Epilogue

After this some operations, in fact, there should be some personal understanding of the package.

Some people also proposed to create a bat to start the java program, the benefits of self-evident, but there are drawbacks. For example: a process in the Task Manager shows the java process, so if there are several such programs do, it will lead to distinguish process. And the need to rely on JRE.

In this way then packed in it, there are many drawbacks, for example, the package was too big, but to solve the drawbacks of the above problems, and can be used in java9 jlink be on jre crop, package size is further reduced, individuals prefer The second way to pack, how to choose, please combine business scenarios and requirements.

About javapackager packaging-related content, please refer to the official website

Encounter problems, please leave a message in the comments below.

Guess you like

Origin juejin.im/post/5e2eba31e51d4558864b1b29