为 Windows/Linux 桌面打包 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Packaging Java apps for the Windows/Linux desktop
提问by alexmcchessers
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.
我正在使用用于 GUI 渲染的 Eclipse SWT 库为桌面编写 Java 应用程序。我认为 SWT 帮助 Java 克服了桌面上接受 Java 的最大障碍:即为 Java 应用程序提供一致的响应式界面,看起来就像属于您桌面上的任何其他应用程序一样。但是,我觉得打包应用程序仍然是一个问题。
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.
OS X 本身提供了一种简单的机制来将 Java 应用程序包装在本机应用程序包中,但是为 Windows/Linux 生成一个不需要用户运行丑陋的批处理文件或单击 .jar 的应用程序仍然很麻烦。可能在 Linux 上这不是一个问题,用户可能更精通技术,但在 Windows 上,我希望有一个常规的 .exe 供他/她运行。
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?
有没有人对现有的任何 Java .exe 生成工具有任何经验?我尝试过 JSmooth,但遇到了各种问题。在我破解 Visual Studio 并推出自己的解决方案之前,是否有更好的解决方案?
Edit:I should perhaps mention that I am unable to spend a lot of money on a commercial solution.
编辑:我或许应该提一下,我无法在商业解决方案上花很多钱。
采纳答案by Brian Kelly
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.
为了跟进 pauxu 的回答,我在我的一个项目中使用了 launch4j 和 NSIS,并认为展示我如何使用它们会有所帮助。这是我为 Windows 所做的。顺便说一句,我正在为 Mac 创建 .app 和 .dmg,但还没有想出为 Linux 做什么。
Project Copies of launch4j and NSIS
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”和“nsis”目录。每个内部都有每个应用程序的安装副本。我发现在项目本地拥有一个副本比强迫其他人安装这两个产品并设置某种环境变量来指向每个产品更容易。
Script Files
脚本文件
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:
我的项目中还有一个“脚本”目录,其中包含我的项目的各种配置/脚本文件。首先是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>
And then there's the NSIS script rpgam-setup.nsis. It can take a VERSION argument to help name the file.
然后是 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 <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" />
register
;readRegStr <target name="executable-windows" depends="jar" description="Create Windows executable (EXE)">
<launch4j configFile="scripts/launch4j.xml" outfile="${exeFile}" />
</target>
HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion
; print the results in a popup message box
;messageBox MB_OK "version: <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>
"
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 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:
我的 Ant 构建文件 (build.xml) 中有一些目标来处理上述问题。首先我 tel Ant 导入 launch4j 的 Ant 任务:
##代码##I then have a simple target for creating the wrapper executable:
然后我有一个简单的目标来创建包装器可执行文件:
##代码##And another target for making the installer:
制作安装程序的另一个目标:
##代码##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.
上面的部分只是将安装程序所需的文件复制到一个临时位置,第二部分执行脚本,使用所有这些文件来制作安装程序。
回答by pauxu
回答by cringe
Maybe you should take a look at IzPack. I created a very nice installer some years ago and I'd bet that they are still improving it. It allows the installation of docs, binaries and a clickable link to start the application IIRC.
也许你应该看看IzPack。几年前我创建了一个非常好的安装程序,我敢打赌他们仍在改进它。它允许安装文档、二进制文件和一个可点击的链接来启动应用程序IIRC。
回答by Johannes K. Lehnert
I've used the free Launch4Jto create a custom launcher for my Java programs on Windows. Combined with the free NSIS Installeryou can build a nice package for your Windows users.
我已经使用免费的Launch4J在 Windows 上为我的 Java 程序创建了一个自定义启动器。结合免费的NSIS 安装程序,您可以为您的 Windows 用户构建一个不错的包。
Edit:Did not see that you use SWT. Don't know if it works with SWT as well, because I used only Swing in my apps.
编辑:没有看到您使用 SWT。不知道它是否也适用于 SWT,因为我在我的应用程序中只使用了 Swing。
回答by Derek Park
Have you considered writing a small program in C/C++ that just calls CreateProcess
to start up the java VM with the jar (or class) file?
您是否考虑过用 C/C++ 编写一个小程序,该程序只调用CreateProcess
使用 jar(或类)文件启动 java VM?
You could get Visual C++ Expressand put together the startup program pretty easily. This would make it easy to add a friendly icon as well.
您可以获得Visual C++ Express并很容易地将启动程序组合在一起。这也可以很容易地添加一个友好的图标。
回答by Heath Borders
Consider converting your application to Eclipse RCP. It is written in SWT, and the Eclipse IDE contains packaging tools that generate executables for all major platforms. For windows, it can generate a zip or a folder containing your code. For a common installation experience, I'd using NSIS. There is actually a packages generatorproject at eclipse to create common installers for all platforms eclipse supports.
考虑将您的应用程序转换为Eclipse RCP。它是用SWT编写的,Eclipse IDE 包含为所有主要平台生成可执行文件的打包工具。对于 Windows,它可以生成包含代码的 zip 或文件夹。对于常见的安装体验,我会使用 NSIS。eclipse实际上有一个包生成器项目,用于为 eclipse 支持的所有平台创建通用安装程序。
回答by Jason Day
Have you thought about Java Web Start? Hereis a tutorial specifically for deploying an SWT application with Java Web Start.
您考虑过Java Web Start吗? 这是一个专门用于使用 Java Web Start 部署 SWT 应用程序的教程。
回答by Mafro34
回答by Richboy
Have you considered Advanced Installer?
I have used it severally especially for Windows and Mac. No scripting or Ant required. All GUI. Very simple and understandable. Ain't free but worth every penny.
- Lauch as Administrator
- File Association
- Custom Install Themes + In built Themes
- Package with JRE
- Install location
- Native Splash screen implementation
- You can event create services and installation events
- Prerequisites
- JRE minimum version and maximum version
您是否考虑过高级安装程序?
我曾多次使用它,特别是在 Windows 和 Mac 上。不需要脚本或 Ant。所有图形用户界面。非常简单易懂。不是免费的,但值得每一分钱。
- 以管理员身份启动
- 文件关联
- 自定义安装主题 + 内置主题
- 使用 JRE 打包
- 安装位置
- 本机启动画面实现
- 您可以创建事件服务和安装事件
- 先决条件
- JRE 最低版本和最高版本
And a lot more. And don't get it twisted, i have no connections with the dudes...their App is just awesome.
还有很多。不要误解,我与这些家伙没有任何联系……他们的应用程序非常棒。