如何为 Java 程序创建 MSI Windows 安装程序?

2022-01-23 00:00:00 java wix windows-installer

我想为 Java 程序创建一个 Microsoft 安装程序文件.我不能使用 Netbeans 或任何 Maven 插件来执行此操作,因为我不能使用 Oracle Java 和/或 JavaFX(主要是出于法律原因),而且两者似乎都使用 Oracle 本机部署 + JavaSE 1.8.

I'd like to create a single Microsoft Installer file for a Java program. I can't use Netbeans or any Maven plugin to do it because I can't use Oracle Java and/or JavaFX (mainly for legal reasons) and both seem to use Oracle native deployment + JavaSE 1.8.

我当前的存档 包含:

  • Java 运行时环境 (OpenJDK 1.7 JRE)
  • 脚本 (.bat)
  • 一个胖 JAR
  • 图标文件
  • 一些文本文件

我不想使用系统的 JRE,所以我提供了另一个 JRE.该脚本仅使用几个参数调用 JRE.图标文件为 .ico 格式.胖 JAR 是包含所有必需的 Java 类、本机库和资产(包括第三方库)的 JAR.

I don't want to use the JRE of the system, that's why I provide another JRE. The script just calls the JRE with a few arguments. The icon file is in .ico format. The fat JAR is the JAR containing all necessary Java classes, native libraries and assets, including the third party libraries.

我想允许最终用户安装、运行和卸载我的程序.我想在开始菜单中提供一个快捷方式来运行它,另一个快捷方式来卸载它.这里似乎有解释.我是否必须使用其他任何东西来创建快捷方式来卸载我的程序?

I'd like to allow the end user to install, run and uninstall my program. I want to provide one shortcut in the start menu to run it and another one to uninstall it. It seems to be explained here. Do I have to use anything else to create a shortcut to uninstall my program?

我已经看了几个星期的 Wix 工具集.我知道我必须编写一个 wxs 文件才能与candle.exe"一起使用,它会创建一个 wixobj 文件,我必须使用该文件运行light.exe"并获得一个 MSI 文件.

I've looked at Wix Toolset for several weeks. I understand that I have to write a wxs file to use with "candle.exe", it creates a wixobj file, I have to run "light.exe" with this file and I get a MSI file.

我知道如何添加文件,这里有说明 但我不知道如何添加包含 JRE 的整个目录而不一一提及每个文件.在wxs文件中怎么做?

I know how to add a file, it's explained here but I don't know how to add the whole directory containing the JRE without mentioning each file one by one. How can it be done in the wxs file?

如何挑选一些独特的 GUID?

How to pick some unique GUIDs?

是否已经有一个(更多?)简单的工具可以用来从一个胖 JAR 制作一个 MSI 文件?在尝试以编程方式在 GNU Linux 下使用 Apache POI 执行相同操作之前,我更喜欢先了解如何在 Windows 下的命令行中构建这样的文件.

Is there already a (more?) simple tool that I can use to make a MSI file from a fat JAR? I prefer understanding how to build such a file in command line under Windows first before trying to do the same programmatically with Apache POI under GNU Linux.

这是我的第一个 wxs 文件:

Edit.: This is my first wxs file:

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product Id="*" Name="Truly Unusual Experience of Revolution" Language="1033" Version="0.0.0.0" Manufacturer="Julien Gouesse" UpgradeCode="00000000-0000-0000-0000-000000000000">
    <!-- Installer's Icon in Add/Remove Programs -->
	<Icon Id="icon.ico" SourceFile="tuerLogo.ico"/>
    <Property Id="ARPPRODUCTICON" Value="icon.ico" />
	<!-- Installer's version = 200 because the 64-bit support is required -->
    <Package InstallerVersion="200" InstallPrivileges="elevated" InstallScope="perMachine" Compressed="yes" Platform="x64" />
    <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
    <MajorUpgrade AllowDowngrades="yes" IgnoreRemoveFailure="yes" Schedule="afterInstallInitialize" />
    <Condition Message="This application is only supported on Windows XP, Windows Vista, Windows Server 2008, or higher.">
      <![CDATA[Installed OR (VersionNT >= 501)]]>
    </Condition>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="APPLICATIONROOTDIRECTORY" Name="Truly Unusual Experience of Revolution">
		  <Component Id="tuer.bat" Guid="00000000-0000-0000-0000-000000000000">
            <File Id="tuer.bat" Source="tuer.bat" KeyPath="yes"/>
		  </Component>
		  <Component Id="tuer.jar" Guid="00000000-0000-0000-0000-000000000000">
            <File Id="tuer.jar" Source="tuer.jar" KeyPath="yes"/>
		  </Component>
		  <Component Id="LICENSE.txt" Guid="00000000-0000-0000-0000-000000000000">
            <File Id="LICENSE.txt" Source="LICENSE.txt" KeyPath="yes"/>
		  </Component>
		  <Component Id="NOTICE.txt" Guid="00000000-0000-0000-0000-000000000000">
            <File Id="NOTICE.txt" Source="NOTICE.txt" KeyPath="yes"/>
		  </Component>
		  <Component Id="README.txt" Guid="00000000-0000-0000-0000-000000000000">
            <File Id="README.txt" Source="README.txt" KeyPath="yes"/>
		  </Component>
		</Directory>
      </Directory>
	  <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="Truly Unusual Experience of Revolution"/>
      </Directory>
	</Directory>
	<!-- Shortcut in the Start Menu -->
	<DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationShortcut" Guid="00000000-0000-0000-0000-000000000000">
        <Shortcut Id="ApplicationStartMenuShortcut" 
                  Name="Truly Unusual Experience of Revolution" 
                  Description="First person shooter"
                  Target="[#tuer.bat]"
                  WorkingDirectory="APPLICATIONROOTDIRECTORY"
			      Icon="icon.ico" />
          <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
          <RegistryValue Root="HKCU" Key="SoftwareMicrosoftTrulyUnusualExperienceofRevolution" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
      </Component>
    </DirectoryRef>
    <Feature Id="TrulyUnusualExperienceofRevolution" Title="Truly Unusual Experience of Revolution" Level="1">
	  <ComponentRef Id="tuer.bat" />
	  <ComponentRef Id="tuer.jar" />
	  <ComponentRef Id="LICENSE.txt" />
	  <ComponentRef Id="NOTICE.txt" />
	  <ComponentRef Id="README.txt" />
	  <ComponentRef Id="ApplicationShortcut" />
	</Feature>
  </Product>
</Wix>

Edit.2:这个是在JRE上运行heat"得到的文件目录.

Edit.2: This is the file obtained by running "heat" on the JRE directory.

注意:我没有回答我自己的问题,但是当我发现使用 NSIS (Nullsoft Scriptable Install System) 会容易很多,因为已经有 一个 Ant 任务 和几个 GNU Linux 发行版的一些软件包(包括 Mageia, Fedora, ...) 使其工作.您可以找到一个名为 Java Launcher 的脚本来在您的软件和操作系统中查找 JRE (即使我建议你在你的软件中捆绑一个 OpenJDK JRE 以确保它能够在更新或操作系统中没有 JRE 的情况下继续工作),另一个自动安装 JRE 的脚本 和 基于 Java 的软件的 nsi 脚本的基本示例.

N.B: I don't answer to my own question but I stopped investigating when I discovered that building an installer with NSIS (Nullsoft Scriptable Install System) would be a lot easier because there are already an Ant task and some packages for several GNU Linux distros (including Mageia, Fedora, ...) to make it work. You can find a script called Java Launcher to look for a JRE in your software and in your operating system (even though I advise you to bundle an OpenJDK JRE in your software to ensure that it will go on working despite the updates or the absence of a JRE in the operating system), another script with automatic JRE installation and a rudimentary example of nsi script for a Java based software.

PS:顺便说一下,我现在使用我自己的工具(它在后台使用 Redline RPM),它有完整的文档,开源(在 GPL 下),适用于 RPM、DEB、APP 和 EXE(通过 NSIS)它也被称为 Java Native Deployment Toolkit.

P.S: By the way, I now use my own tool (which uses Redline RPM under the hood), it's fully documented, open source (under GPL), and works for RPM, DEB, APP and EXE (via NSIS) too, it's called Java Native Deployment Toolkit.

推荐答案

升级到 Java 14+ 并使用内置的 jpackage 工具(它是来自 Jave 8 的 'jpackager' 的后代/9).像大多数一样,它会为您运行它的系统生成软件包:

Upgrade to Java 14+ and use the built-in jpackage tool (which is the descendant of the 'jpackager' from Jave 8/9). Like most, it generates packages for the system you run it on:

  1. Linux:deb 和 rpm
  2. macOS:pkg 和 dmg
  3. Windows:msi 和 exe

它使用 jdeps 和 jlink 构建最小的 JRE 以包含在安装程序中,从而避免了单独下载 Java 的需要.另外,每次 JDK 下载都是免费的.感谢 Kevin Rushforth 和 JEP343 团队!在 https://bugs.openjdk.java.net/browse/JDK 阅读开发日志-8200758 或执行 jpackage --help.请注意,从 Java14 开始,它在技术上仍处于孵化"阶段.所以一些功能可能会改变.

It uses jdeps and jlink to build a minimal JRE to include in the installer, avoiding the need to separately download Java. Plus, it's free with every JDK download. Thanks to Kevin Rushforth and the JEP343 team! Read the development log at https://bugs.openjdk.java.net/browse/JDK-8200758 or do jpackage --help. Note that, as of Java14, it's technically still in "incubation" so some features might change.

相关文章