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

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

Packaging Java apps for the Windows/Linux desktop

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

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

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

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


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

launch4j和NSIS的项目副本

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

脚本文件

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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参数来帮助命名文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
; 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
pgam-${VERSION}.exe"


; The default installation directory
InstallDir"$PROGRAMFILES
PG Audio Mixer"


; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM"Software
PG_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
PG_Audio_Mixer"Install_Dir""$INSTDIR"

    ; Write the uninstall keys for Windows
    WriteRegStr HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall
PGAudioMixer"
"DisplayName""RPG Audio Mixer"
    WriteRegStr HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall
PGAudioMixer"
"UninstallString" '"$INSTDIR\uninstall.exe"'
    WriteRegDWORD HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall
PGAudioMixer"
"NoModify" 1
    WriteRegDWORD HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall
PGAudioMixer"
"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
PG Audio Mixer"

  CreateShortCut"$SMPROGRAMS
PG Audio Mixer\Uninstall.lnk"
"$INSTDIR\uninstall.exe""""$INSTDIR\uninstall.exe" 0
  CreateShortCut"$SMPROGRAMS
PG AUdio Mixer
PG Audio Mixer.lnk"
"$INSTDIR
pgam.exe"
"""$INSTDIR
pgam.exe"
0
SectionEnd

Section"Uninstall"

    ; Remove registry keys
    DeleteRegKey HKLM"Software\Microsoft\Windows\CurrentVersion\Uninstall
PGAudioMixer"

    DeleteRegKey HKLM SOFTWARE
PG_Audio_Mixer

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

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


    ; Remove directories used
    RMDir"$SMPROGRAMS
PG Audio Mixer"

    RMDir"$INSTDIR"

SectionEnd

蚂蚁整合

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

1
2
3
4
<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" />

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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">
       
       
    </exec>
</target>

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


在我公司中,我们使用Launch4J来创建exe文件,并使用NSIS来创建带有SWT应用程序的安装程序。

多年来,我们已将其用于多种商业应用中,并且效果很好。


也许您应该看看IzPack。几年前,我创建了一个非常不错的安装程序,我敢打赌他们仍在改进它。它允许安装文档,二进制文件和可单击链接来启动应用程序IIRC。


我已经使用免费的Launch4J为Windows上的Java程序创建了一个自定义启动器。结合免费的NSIS安装程序,您可以为Windows用户构建一个不错的软件包。

编辑:没有看到您使用SWT。不知道它是否也适用于SWT,因为我在应用程序中仅使用了Swing。


您是否考虑过用C / C ++编写一个小的程序,该程序仅调用CreateProcess来使用jar(或类)文件启动Java VM?

您可以使用Visual C ++ Express并轻松组合启动程序。这样也可以轻松添加友好的图标。


考虑将您的应用程序转换为Eclipse RCP。它是用SWT编写的,并且Eclipse IDE包含打包工具,可以为所有主要平台生成可执行文件。对于Windows,它可以生成一个包含您的代码的zip或文件夹。对于一般的安装体验,我将使用NSIS。 eclipse实际上有一个软件包生成器项目,可以为eclipse支持的所有平台创建通用的安装程序。


您是否考虑过Java Web Start?这是专门用于通过Java Web Start部署SWT应用程序的教程。


Install4J。不是免费的,但是值得。试用一下


您现在可以通过Netbeans做到这一点!这真的很容易并且完美地工作。在Netbeans网站上查看本教程。


我过去使用过JSmooth,但仍然很幸运。用户界面非常有缺陷,但是我只用它一次构建配置文件,然后在那之后从Ant构建。

JSmooth有什么问题?


JSMooth在生产环境中为我们工作得非常好,在该环境中,我首先使用一个jar(用于胖子的胖jar插件)生成了一个jar,然后用JSmooth将其包装。

(请注意,我想要一个没有文件的免安装发行版,如果需要的话,可以提示安装JRE)。

它运行得很好,以至于我认为没有人使用它:)


您考虑过高级安装程序吗?

我已经多次使用它,尤其是在Windows和Mac上。无需脚本或Ant。所有GUI。非常简单易懂。不是免费的,但值得每一分钱。

-劳厄(Lauch)作为管理员
-文件关联
-自定义安装主题+内置主题
-与JRE打包
-安装位置
-本机启动画面实现
-您可以事件创建服务和安装事件
-先决条件
-JRE的最低版本和最高版本

还有更多。而且不要扭曲它,我与帅哥没有任何关系...他们的应用程序真棒。


您可能要尝试使用我们的工具BitRock InstallBuilder。尽管它是本机应用程序,但我们的许多客户都使用它来打包桌面Java应用程序。如果捆绑JRE并创建启动器等,则用户甚至不需要知道他们正在安装Java应用程序。它是跨平台的,因此您可以为Windows和Mac(以及Linux,Solaris等)生成安装程序。就像另一篇文章中提到的install4j工具一样,它是一种商业工具,但是我们为开源项目提供免费许可证,并提供特殊折扣对于microISV /小型企业等,请给我们发送电子邮件。还想强调一下,这是一个安装程序工具,因此,如果您仅寻找单个文件可执行文件,它将无法满足您的需求。


在我的公司中,我们将launch4J和NSIS用于Windows发行版,将jdeb用于Debian发行版,并将Java Web Start用于常规操作系统。这工作得很好。


我正在考虑的另一种选择:Eclipse并非从头开始编写本机启动器,而是随附了其自己的启动器的源代码,并且也许可以将其用于我的应用程序。

Sun从来没有在JDK中包含任何类似的东西,这是可耻的。


请尝试InstallJammer。这是我用过的最好的。免费且功能强大,足以用于个人和商业用途。


我经历了同样的事情,发现所有的免费选项都不是很好。看来您将自己编写。我想看看有人是否有可用的免费/廉价选项


对Launch4J的另一票表决是,今天早晨刚刚写了一个与我的项目集成的蚂蚁任务。看起来真的很好


推荐阅读