代码之家  ›  专栏  ›  技术社区  ›  alexmcchessers

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

  •  36
  • alexmcchessers  · 技术社区  · 16 年前

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

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

    编辑:

    18 回复  |  直到 15 年前
        1
  •  32
  •   Brian Kelly    15 年前

    为了跟进pauxu的回答,我在我的一个项目中使用了launch4j和NSIS,我认为展示一下我是如何使用它们会很有帮助。下面是我为Windows所做的。顺便说一句,我正在为Mac创建.app和.dmg,但还没有弄清楚Linux应该做什么。

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

    脚本文件

    我的项目中还有一个“scripts”目录,其中保存了项目的各种配置/脚本文件。首先是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。它可以使用版本参数来帮助命名文件。

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

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

        2
  •  10
  •   pauxu    16 年前

    在我的公司里,我们使用 Launch4J 创建exe文件,以及 NSIS 使用SWT应用程序创建安装程序。

    多年来,我们已经在多个商业应用中使用了它,这对产品运行良好。

        3
  •  7
  •   cringe    16 年前

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

        4
  •  4
  •   Johannes K. Lehnert    16 年前

    我用过免费的 Launch4J 在Windows上为我的Java程序创建自定义启动器。结合免费 NSIS Installer 您可以为您的Windows用户构建一个很好的软件包。

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

        5
  •  3
  •   Derek Park    16 年前

    您是否考虑过用C/C++编写一个只调用 CreateProcess 要用jar(或类)文件启动JavaVM?

    你可以得到 Visual C++ Express 并且很容易地完成启动程序。这也使得添加友好图标变得容易。

        6
  •  3
  •   Heath Borders    16 年前

    考虑将应用程序转换为 Eclipse RCP . 它是用英文写的 SWT ,EclipseIDE包含为所有主要平台生成可执行文件的打包工具。对于windows,它可以生成包含代码的zip或文件夹。对于常见的安装体验,我建议使用NSIS。实际上有一个 packages generator eclipse上的项目,为eclipse支持的所有平台创建通用安装程序。

        7
  •  2
  •   Jason Day    16 年前

    你有没有想过 Java Web Start Here 是一个专门用于使用JavaWebStart部署SWT应用程序的教程。

        8
  •  1
  •   basszero    16 年前

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

        9
  •  1
  •   Mafro34    11 年前

    您现在可以通过Netbeans实现这一点了!这真的很简单,而且效果很好。退房 this Netbeans网站上的教程。

        10
  •  0
  •   basszero    16 年前

        11
  •  0
  •   alexmcchessers    16 年前

    我正在考虑的另一个选择是:Eclipse自带了自己的启动器的源代码,而不是从头开始编写本机启动器,这可能会重新用于我的应用程序。

        12
  •  0
  •   JohnnyOPSU JohnnyOPSU    16 年前

    又一次投票支持Launch4J,今天早上刚刚写了一个ant任务,与我的一个项目集成。看起来效果很好

        13
  •  0
  •   James Van Huis    16 年前

    我在过去使用过JSmooth,现在仍然很幸运。UI有很多错误,但我只在构建配置文件时使用它一次,然后再从Ant构建。

    你对JSmooth有什么问题?

        14
  •  0
  •   Thorbjørn Ravn Andersen    16 年前

    JSMooth在生产环境中工作得非常好,我首先使用一个jar(eclipse的fatjar插件)生成了一个jar,然后用JSMooth包装它。

    (请注意,我想要一个单一文件的无安装发行版,如果需要的话,它可以促进JRE的安装)。

    它工作得如此之好,以至于我以为没有人在使用它:)

        15
  •  0
  •   Daniel Lopez    16 年前

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

        16
  •  0
  •   Gatorhall    15 年前

    在我的公司,windows发行版使用launch4J和NSIS,Debian发行版使用jdeb,通用操作系统使用JavaWebStart。这个很好用。

        17
  •  0
  •   Munim    13 年前

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

        18
  •  0
  •   Richboy    10 年前





    -Lauch作为管理员

    -自定义安装主题+内置主题
    -带有JRE的包
    -安装位置
    -本机启动屏幕实现
    -您可以创建事件服务和安装事件

    -JRE最低版本和最高版本