打包用于Windows / Linux桌面的Java应用程序

alexmcchessers:

我正在使用Eclipse SWT库进行GUI渲染,用Java为桌面编写一个应用程序。我认为SWT帮助Java克服了在桌面上接受的最大障碍:即为Java应用程序提供一致的,响应式的界面,该界面看起来与桌面上其他任何应用程序的界面都一样。但是,我觉得打包应用程序仍然是一个问题。

OS X本机提供了一种将Java应用程序包装在本机应用程序捆绑包中的简单机制,但是为Windows / Linux生成不需要用户运行丑陋的批处理文件或单击.jar的应用程序仍然很麻烦。在Linux上,这可能不是问题,在Linux上,用户可能更精通技术,但是在Windows上,我希望有一个常规的.exe供他/她运行。

有没有人对使用Java的.exe生成工具有任何经验?我尝试了JSmooth,但是遇到了各种各样的问题。在我推出Visual Studio并推出自己的产品之前,还有更好的解决方案吗?

编辑:也许我应该提到我无法在商业解决方案上花费很多钱。

布莱恩·凯利(Brian Kelly):

为了跟进pauxu的回答,我在我的一个项目中使用了launch4j和NSIS,并认为展示我的使用方式将很有帮助。这是我为Windows做的事情。顺便说一句,我正在为Mac创建.app和.dmg,但是还没有弄清楚Linux怎么做。

launch4j和NSIS的项目副本

在我的项目中,我有一个“供应商”目录,在它的下面有一个“ launch4j”和“ nsis”目录。每个文件中都有每个应用程序的安装副本。我发现在项目本地拥有一个副本比强迫其他人安装两个产品并设置某种指向每个产品的环境变量要容易得多。

脚本文件

我的项目中还有一个“脚本”目录,其中包含项目的各种配置/脚本文件。首先是launch4j.xml文件:

<launch4jConfig>
  <dontWrapJar>true</dontWrapJar>
  <headerType>gui</headerType>
  <jar>rpgam.jar</jar>
  <outfile>rpgam.exe</outfile>
  <errTitle></errTitle>
  <cmdLine></cmdLine>
  <chdir>.</chdir>
  <priority>normal</priority>
  <downloadUrl>http://www.rpgaudiomixer.com/</downloadUrl>
  <supportUrl></supportUrl>
  <customProcName>false</customProcName>
  <stayAlive>false</stayAlive>
  <manifest></manifest>
  <icon></icon>
  <jre>
    <path></path>
    <minVersion>1.5.0</minVersion>
    <maxVersion></maxVersion>
    <jdkPreference>preferJre</jdkPreference>
  </jre>
  <splash>
    <file>..\images\splash.bmp</file>
    <waitForWindow>true</waitForWindow>
    <timeout>60</timeout>
    <timeoutErr>true</timeoutErr>
  </splash>
</launch4jConfig>

然后是NSIS脚本rpgam-setup.nsis。可以使用VERSION参数来帮助命名文件。

; The name of the installer
Name "RPG Audio Mixer"

!ifndef VERSION
    !define VERSION A.B.C
!endif

; The file to write
outfile "..\dist\installers\windows\rpgam-${VERSION}.exe"

; The default installation directory
InstallDir "$PROGRAMFILES\RPG Audio Mixer"

; Registry key to check for directory (so if you install again, it will 
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\RPG_Audio_Mixer" "Install_Dir"

# create a default section.
section "RPG Audio Mixer"

    SectionIn RO

    ; Set output path to the installation directory.
    SetOutPath $INSTDIR
    File /r "..\dist\layout\windows\"

    ; Write the installation path into the registry
    WriteRegStr HKLM SOFTWARE\RPG_Audio_Mixer "Install_Dir" "$INSTDIR"

    ; Write the uninstall keys for Windows
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RPGAudioMixer" "DisplayName" "RPG Audio Mixer"
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RPGAudioMixer" "UninstallString" '"$INSTDIR\uninstall.exe"'
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RPGAudioMixer" "NoModify" 1
    WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RPGAudioMixer" "NoRepair" 1
    WriteUninstaller "uninstall.exe"

    ; read the value from the registry into the $0 register
    ;readRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion

    ; print the results in a popup message box
    ;messageBox MB_OK "version: $0"

sectionEnd

Section "Start Menu Shortcuts"
  CreateDirectory "$SMPROGRAMS\RPG Audio Mixer"
  CreateShortCut "$SMPROGRAMS\RPG Audio Mixer\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortCut "$SMPROGRAMS\RPG AUdio Mixer\RPG Audio Mixer.lnk" "$INSTDIR\rpgam.exe" "" "$INSTDIR\rpgam.exe" 0
SectionEnd

Section "Uninstall"

    ; Remove registry keys
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\RPGAudioMixer"
    DeleteRegKey HKLM SOFTWARE\RPG_Audio_Mixer

    ; Remove files and uninstaller
    Delete $INSTDIR\rpgam.exe
    Delete $INSTDIR\uninstall.exe

    ; Remove shortcuts, if any
    Delete "$SMPROGRAMS\RPG Audio Mixer\*.*"

    ; Remove directories used
    RMDir "$SMPROGRAMS\RPG Audio Mixer"
    RMDir "$INSTDIR"

SectionEnd

蚂蚁整合

我的Ant构建文件(build.xml)中有一些目标可以处理上述问题。首先,我打电话给Ant导入launch4j的Ant任务:

<property name="launch4j.dir" location="vendor/launch4j" />
<taskdef name="launch4j" 
    classname="net.sf.launch4j.ant.Launch4jTask"
    classpath="${launch4j.dir}/launch4j.jar:${launch4j.dir}/lib/xstream.jar" />

然后,我有了一个用于创建包装可执行文件的简单目标:

<target name="executable-windows" depends="jar" description="Create Windows executable (EXE)">
    <launch4j configFile="scripts/launch4j.xml" outfile="${exeFile}" />
</target>

制作安装程序的另一个目标:

<target name="installer-windows" depends="executable-windows" description="Create the installer for Windows (EXE)">
    <!-- Lay out files needed for building the installer -->
    <mkdir dir="${windowsLayoutDirectory}" />
    <copy file="${jarFile}" todir="${windowsLayoutDirectory}" />
    <copy todir="${windowsLayoutDirectory}/lib">
        <fileset dir="${libraryDirectory}" />
        <fileset dir="${windowsLibraryDirectory}" />
    </copy>
    <copy todir="${windowsLayoutDirectory}/icons">
         <fileset dir="${iconsDirectory}" />
    </copy>
    <copy todir="${windowsLayoutDirectory}" file="${exeFile}" />

    <mkdir dir="${windowsInstallerDirectory}" />

    <!-- Build the installer using NSIS -->
    <exec executable="vendor/nsis/makensis.exe">
        <arg value="/DVERSION=${version}" />
        <arg value="scripts/rpgam-setup.nsi" />
    </exec>
</target>

上面的部分只是将安装程序所需的文件复制到一个临时位置,而下半部分执行使用所有脚本制作安装程序的脚本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何打包Java桌面应用程序?

打包Java桌面应用程序(gradle)

Mac、Windows 和 Linux 上的 Java 桌面应用程序安装程序

用于Ubuntu的打包-Web应用程序

跨平台桌面应用程序-Windows + Mac + Linux

适用于Java桌面应用程序的MVC

用于桌面应用程序开发的Java框架

用于纯 Java 桌面应用程序的分析服务/API

是否有适用于ubuntu桌面的类似iPhone的提醒应用程序?

适用于桌面应用程序的Java Swing或Windows Forms?

miktex和pandoc的相对系统路径-闪亮的应用程序打包为Windows桌面应用程序

Windows桌面应用程序的Xamarin表单?

应用程序图标不适用于 Snap 打包

如何打包适用于iOS的Kivy应用程序?

是否有适用于Linux的MongoDB GUI桌面应用程序?

创建适用于Windows的App安装程序-电子(电子打包程序,电子生成器)

Windows 8应用Java编写的Windows桌面应用程序的认证

如何将Windows登录用于桌面Java应用程序的单点登录和Active Directory条目?

打包个人Windows通用应用程序windows.fullTrustProcess

将Node.js Web应用程序打包为普通的桌面应用程序

什么是用于Java桌面应用程序的最佳数据库

适用于Java桌面应用程序的JavaFX,Swing或Flex

需要用于桌面应用程序的现代Java概述

我可以将Room Persistence Library用于Java桌面应用程序吗?

在带有 WSL2 的 Windows 10 Docker 桌面中,docker 容器在哪里以及 Linux 容器如何运行 java 应用程序,而不是 windows nanoserver?

在 Linux 中使用 jenkins 和 electron-packager 为 windows 打包电子应用程序

javapackager 可以从 Mac 打包 Windows 应用程序吗?

Windows打包的Ruby Shoes应用程序不显示图像

我可以将Windows存储应用程序C#代码(命名空间)用于桌面应用程序吗?