1.满足于Jar吗?
写了个java桌面程序,封装成了.jar文件可以双击运行。满足于此吗?这个程序看起来不像一个Windows 应用程序。首先,进程名称是javaw.exe,而不是你自定义的进程名称。没有图标。不是.exe可执行文件,用户开始看到会犹豫,这玩意能不能运行?
2.封装jar 成 exe
先把jar做成exe程序,有很多开源软件可供选择,这里推荐launch4j。网上拷贝一份配置文件,改改,就可以了。下面是简单的launch4j配置文件:
<launch4jConfig> <headerType>gui</headerType> <!--gui程序,若填console,程序运行时,会有一个命令行跟着启动;即使是后台应用程序,也建议用gui--> <outfile>a.exe</outfile> <!--输出文件,不用多说,这里指定文件名即可--> <jar>lib/xx.jar</jar> <!--很重要,你的main函数在那个jar包中--> <dontWrapJar>false</dontWrapJar> <!--这里选false,将jar封装成exe二进制格式--> <downloadUrl>http://java.com/download</downloadUrl> <!--如果机器上没有jre,提示用户到这里去下载--> <icon>xx.ico</icon> <!--支持程序图标--> <customProcName>true</customProcName> <classPath> <mainClass>MainClass</mainClass> <cp>lib/*.jar</cp> <!--这个很重要,指定classpath--> </classPath> <singleInstance> <mutexName>xx</mutexName> <windowTitle>text</windowTitle> </singleInstance> <!--是否允许多实例运行--> <jre> <minVersion>1.6.0</minVersion> <maxVersion>1.7.0</maxVersion> <jdkPreference>preferJre</jdkPreference> <!--指定依赖的jre环境,最低,最高版本--> </jre> </launch4jConfig>
launch4j支持以ant任务的方式执行封装任务,方便自动化。下面是简单的ant任务配置:
<property name="launch4j.dir" location="path-to-launch4j/" /> <target name="exe" depends="junit"> <taskdef name="launch4j" classname="net.sf.launch4j.ant.Launch4jTask" classpath="${launch4j.dir}/launch4j.jar :${launch4j.dir}/lib/xstream.jar" /> <launch4j configFile="launch4j.xml" /> </target>
3.已经是exe文件了,不过是绿色软件,有时候需要做一个安装程序setup.exe,为程序执行准备环境,如制作成 windows 服务开机运行,或者为用户创建快捷方式等等。这里推荐功能强大的InnoSetup,自己写一个脚本,自动完成封装。下面是简单的Inno Script脚本文件:
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "A Name"
#define MyAppVersion "1.0"
#define MyAppPublisher "xx, Inc."
#define MyAppURL "http://www.xxx.com/"
#define MyAppExeName "xxx.exe"
[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{id-generated}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "xx.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "xx.xml"; DestDir: "{app}"; Flags: ignoreversion
Source: "lib\*"; DestDir: "{app}\lib"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
;这里配置快捷方式
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
上面的文件挺复杂的?我也没有解释,呵呵,因为上面的内容基本上是InnoSetup自动生成的,在InnoSetup Compiler里,新建iss文件,会有向导一步一步提示。OK,完成了,马上跑一下setup.exe,现在,你的java程序,已经变得很专业了。