java - 为 Windows/Linux 桌面打包 Java 应用程序

标签 java windows swt executable software-distribution

我正在使用用于 GUI 呈现的 Eclipse SWT 库用 Java 编写桌面应用程序。我认为 SWT 帮助 Java 克服桌面上接受的最大障碍:即为 Java 应用程序提供一个一致的、响应式的界面,看起来就像属于你桌面上的任何其他应用程序一样。但是,我觉得打包一个应用程序仍然是一个问题。

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

有没有人使用过任何用于 Java 的 .exe 生成工具?我尝试过 JSmooth,但遇到了各种问题。在我破解 Visual Studio 并自己动手之前有没有更好的解决方案?

编辑:我或许应该提一下,我无法在商业解决方案上花很多钱。

最佳答案

为了跟进 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 集成

我的 Ant 构建文件 (build.xml) 中有一些目标来处理上述问题。首先我 tel 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>

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

关于java - 为 Windows/Linux 桌面打包 Java 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720/

相关文章:

java - 从 swt 浏览器(mozilla)运行小程序嵌入页面

java - 如何在 Eclipse 中制作方形编辑器选项卡(无 swt-border-radius)?

Java oop 继承和接口(interface)

ruby-on-rails - 间歇性慢速 rails 应用程序

c++ - 使用 cygwin 为 Windows 构建静态库

c++ - 为什么我的 EnumWindowProc 不能用 C 编译?

java - 简化代码以根据位置获取最小值、最大值和精确值

java - Selenium 的范围报告中出现错误

java - Selenium 中 try catch block 中带有 isDisplayed() 方法的 NoSuchElementException

java - 防止在文本字段上滚动? Java SWT