打包Windows / Linux桌面的Java应用程序 [英] Packaging Java apps for the Windows/Linux desktop

查看:134
本文介绍了打包Windows / Linux桌面的Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing an application in Java for the desktop using the Eclipse SWT library for GUI rendering. I think SWT helps Java get over the biggest hurdle for acceptance on the desktop: namely providing a Java application with a consistent, responsive interface that looks like that belonging to any other app on your desktop. However, I feel that packaging an application is still an issue.

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

OS X natively provides an easy mechanism for wrapping Java apps in native application bundles, but producing an app for Windows/Linux that doesn't require the user to run an ugly batch file or click on a .jar is still a hassle. Possibly that's not such an issue on Linux, where the user is likely to be a little more tech-savvy, but on Windows I'd like to have a regular .exe for him/her to run.

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

Has anyone had any experience with any of the .exe generation tools for Java that are out there? I've tried JSmooth but had various issues with it. Is there a better solution before I crack out Visual Studio and roll my own?

编辑:我或许应该提到我无法支出商业解决方案上有很多钱。

I should perhaps mention that I am unable to spend a lot of money on a commercial solution.

推荐答案

为了跟进pauxu的回答,我正在使用launch4j和NSIS我的项目并认为展示我如何使用它们会很有帮助。这就是我正在为Windows做的事情。顺便说一句,我正在为Mac创建.app和.dmg,但还没想出要为Linux做什么。

To follow up on pauxu's answer, I'm using launch4j and NSIS on a project of mine and thought it would be helpful to show just how I'm using them. Here's what I'm doing for Windows. BTW, I'm creating .app and .dmg for Mac, but haven't figured out what to do for Linux yet.

在我的项目中,我有一个供应商目录,在它下面我有一个launch4j和nsis的目录。每个应用程序中都有一份安装副本。我发现在项目本地复制更容易,而不是强迫其他人安装这两个产品并设置某种环境变量来指向每个产品。

In my project I have a "vendor" directory and underneath it I have a directory for "launch4j" and "nsis". Within each is a copy of the install for each application. I find it easier to have a copy local to the project rather than forcing others to install both products and set up some kind of environment variable to point to each.

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

I also have a "scripts" directory in my project that holds various configuration/script files for my project. First there is the launch4j.xml file:

<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参数来帮助命名文件。

And then there's the NSIS script rpgam-setup.nsis. It can take a VERSION argument to help name the file.

; 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 tel来导入launch4j的Ant任务:

Ant Integration

I have some targets in my Ant buildfile (build.xml) to handle the above. First I tel Ant to import launch4j's Ant tasks:

<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" />

然后我有一个简单的目标来创建包装器可执行文件:

I then have a simple target for creating the wrapper executable:

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

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

And another target for making the installer:

<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>

它的顶部只是将安装程序所需的文件复制到临时位置,下半部分执行使用所有脚本来创建安装程序的脚本。

The top portion of that just copies the necessary files for the installer to a temporary location and the second half executes the script that uses all of it to make the installer.

这篇关于打包Windows / Linux桌面的Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆