制作java软件安装包

| 分类 技术随笔 

java程序开发好了,接下来要进行打包分发工作了。

java自带javapackage可以进行打包。这里我们使用launch4j将其转换为可执行文件。launch4j是跨平台的,但只能生成windows程序。

Windows

用launch4j生成可执行文件

以下是我的launch4j配置文件,可以在图形界面下打开修改:

<?xml version="1.0" encoding="UTF-8"?>
<launch4jConfig>
  <dontWrapJar>true</dontWrapJar>  <!-- 选择true,那么jar不会包含在exe里,exe主要是运行java -jar命令 -->
  <headerType>gui</headerType>
  <jar>BasicCAT.jar</jar>
  <outfile>C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\BasicCAT.exe</outfile>
  <errTitle></errTitle>
  <cmdLine></cmdLine>
  <chdir></chdir>
  <priority>normal</priority>
  <downloadUrl>http://java.com/download</downloadUrl>
  <supportUrl></supportUrl>
  <stayAlive>false</stayAlive>
  <restartOnCrash>false</restartOnCrash>
  <manifest></manifest>
  <icon>C:\Users\xulihang\Downloads\basiccat_1.ico</icon>
  <jre>
    <path>jre1.8.0_202</path>  <!-- jre的位置,这里是相对路劲。如果不存在就使用系统自带的jre-->
    <bundledJre64Bit>false</bundledJre64Bit>
    <bundledJreAsFallback>false</bundledJreAsFallback>
    <minVersion>1.7.0_06</minVersion>  <!-- 最低需要的jre版本-->
    <maxVersion></maxVersion>
    <jdkPreference>preferJre</jdkPreference>
    <runtimeBits>64/32</runtimeBits>
  </jre>
</launch4jConfig>

这样可以生成一个exe文件,生成的exe可以传递参数(commandline args)到java程序。

用inno setep制作安装包

我们可以用inno setep制作exe安装包。

以下是我的iss脚本文件:

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId=\{\{6974B743-549E-4773-9B7A-0EFE71A61295\}
AppName=BasicCAT
AppVersion=1.0
;AppVerName=BasicCAT 1.0
AppPublisher=Xu Lihang
AppPublisherURL=http://blog.xulihang.me
AppSupportURL=http://blog.xulihang.me
AppUpdatesURL=http://blog.xulihang.me
DefaultDirName={pf}\BasicCAT
DisableProgramGroupPage=yes
OutputBaseFilename=setup
SetupIconFile=C:\Users\xulihang\Downloads\basiccat_1.ico
Compression=lzma
SolidCompression=yes

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\BasicCAT.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\BasicCAT.jar"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\model\*"; DestDir: "{app}\model"; Flags: ignoreversion
Source: "C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\plugins\*"; DestDir: "{app}\plugins"; Flags: ignoreversion
Source: "C:\Users\xulihang\Documents\B4J\BasicCAT\Objects\BasicCAT\jre1.8.0_202\*"; DestDir: "{app}\jre1.8.0_202"; Flags: ignoreversion recursesubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
; 使用recursesubdirs,可以复制所有子文件夹

[Icons]
Name: "{commonprograms}\BasicCAT"; Filename: "{app}\BasicCAT.exe"
Name: "{commondesktop}\BasicCAT"; Filename: "{app}\BasicCAT.exe"; Tasks: desktopicon

[Run]
Filename: "{app}\BasicCAT.exe"; Description: "{cm:LaunchProgram,BasicCAT}"; Flags: nowait postinstall skipifsilent

生成的exe可以用以下参数进行静默安装:

basiccat.exe /verysilent /suppressmsgboxes

发布到Windows Store

利用桌面桥,可以将桌面Windows程序发布到Windows Store上。微软现在提供两种方法,一种是使用Desktop App Converter,一种是使用Visual Studio,后者更加方便一点。

macOS

launch4j只能生成exe文件,在mac上我们可以用jdk自带的javapackager。

怎么做可以参考这篇文章以及官方文档

javapackager \
  -deploy -Bruntime=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home \
  -native image \
  -srcdir java \
  -srcfiles BasicCAT.jar \
  -outdir release \
  -outfile BasicCAT \
  -appclass org.xulihang.basiccat.main \
  -name BasicCAT \
  -title BasicCAT \
  -nosign \
  -v

通过以上命令,可以生成一个.app文件。.app就是个文件夹,我们可以把需要的文件再补充进去。比如动态库、icns图标等等。最后使用磁盘工具建立dmg文件即可。如果要在Windows上操作dmg,可以使用TransMAC。

javapackager也可以生成deb和rpm等linux系统的安装包。


上一篇     下一篇