这听起来像是您不需要的东西,但是有时候,当您分发最终用户软件时,可能需要将Java程序安装为Windows服务。 我之所以必须这样做,是因为我开发了一种用于公务员的工具 ,可以自动将其Excel文件转换并将其推入我国的opendata门户。 该工具必须定期运行,因此它是服务的主要候选者(即使公务员完全忘记了此任务,这也可以使上传成为可能,此外,重复的手动上传是浪费时间)。
尽管有很多关于该主题的文章和stackoverflow答案,但是由于一些小的警告和很少有人看到的一个重要先决条件,我仍然花了很多时间–捆绑了JRE,因此没有人需要下载并安装一个JRE(不必要地使安装过程复杂化,并且目标受众不一定精通技术)。
因此,对于带有jar打包的Maven项目,我首先想到了打包一个exe(带有launch4j ),然后将其注册为服务。 这样做的问题是Java程序使用调度的执行程序,因此它永远不会退出,这使它无法作为进程启动。
因此,我不得不使用commons-daemon procrun“对其进行守护” 。 在此之前,我必须将所需的每个组件组装到一个目标文件夹中,即胖子jar(包括所有依赖项),JRE,commons-daemon二进制文件和配置文件。
您可以在此处查看完整的maven文件 。 相关位是(其中${installer.dir}
是${project.basedir}/target/installer}
):
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target></configuration>
</plugin>
<plugin><artifactId>maven-assembly-plugin</artifactId><executions><execution><id>assembly</id><phase>package</phase><goals><goal>single</goal></goals><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><finalName>opendata-ckan-pusher</finalName><appendAssemblyId>false</appendAssemblyId></configuration></execution></executions>
</plugin>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.7</version><executions><execution><id>default-cli</id><phase>package</phase><goals><goal>run</goal></goals><configuration><target><copy todir="${installer.dir}/jre1.8.0_91"><fileset dir="${project.basedir}/jre1.8.0_91" /></copy><copy todir="${installer.dir}/commons-daemon"><fileset dir="${project.basedir}/commons-daemon" /></copy><copy file="${project.build.directory}/opendata-ckan-pusher.jar" todir="${installer.dir}" /><copy file="${project.basedir}/install.bat" todir="${installer.dir}" /><copy file="${project.basedir}/uninstall.bat" todir="${installer.dir}" /><copy file="${project.basedir}/config/pusher.yml" todir="${installer.dir}" /><copy file="${project.basedir}/LICENSE" todir="${installer.dir}" /></target></configuration></execution></executions>
</plugin>
您将注意到installer.bat和uninstaller.bat,它们是使用commons-daemon管理服务的文件。 安装程序将创建服务。 Commons-daemon具有三种模式:exe(允许您包装任意可执行文件),Java(类似于exe,但适用于Java应用程序)和jvm(以相同的过程运行Java应用程序;我不知道如何完全一样)。
我可以使用所有三个选项(包括launch4j创建的exe),但是jvm允许您使用指定的方法来控制正在运行的应用程序。 StartClass / StartMethod / StopClass / StopMethod参数适用于此。 这是整个installer.bat:
commons-daemon\prunsrv //IS//OpenDataPusher --DisplayName="OpenData Pusher" --Description="OpenData Pusher"^--Install="%cd%\commons-daemon\prunsrv.exe" --Jvm="%cd%\jre1.8.0_91\bin\client\jvm.dll" --StartMode=jvm --StopMode=jvm^--Startup=auto --StartClass=bg.government.opendatapusher.Pusher --StopClass=bg.government.opendatapusher.Pusher^--StartParams=start --StopParams=stop --StartMethod=windowsService --StopMethod=windowsService^--Classpath="%cd%\opendata-ckan-pusher.jar" --LogLevel=DEBUG^ --LogPath="%cd%\logs" --LogPrefix=procrun.log^--StdOutput="%cd%\logs\stdout.log" --StdError="%cd%\logs\stderr.log"commons-daemon\prunsrv //ES//OpenDataPusher
一些澄清:
- Jvm参数指向jvm dll(老实说,如果机器上没有其他Java安装,我不确定这是否可以工作;应该)
- StartClass / StartMethod / StopClass / StopMethod指向用于控制正在运行的应用程序的指定方法。 在这种情况下,启动将仅调用main方法,而停止将关闭计划的执行程序,以便应用程序可以退出
- classpath参数指向胖子
- 使用%cd%确定当前目录的路径是有风险的,但是由于最终用户将始终从其所在目录启动它,因此在这种情况下是安全的。
windowsService
看起来像这样:
public static void windowsService(String args[]) throws Exception {String cmd = "start";if (args.length > 0) {cmd = args[0];}if ("start".equals(cmd)) {Pusher.main(new String[]{});} else {executor.shutdownNow();System.exit(0);}
}
这里的一个重要说明是您可能遇到的32位/ 64位问题。 这就是为什么捆绑32位JRE并使用32位(默认)prunsrv.exe更安全的原因。
然后,我有了一个“安装程序”文件夹,其中包含jre和commons-daemon文件夹,以及两个bat文件和一个胖罐。 然后,我可以将其打包为可自解压的归档文件,并进行分发(当然需要使用手册)。 我也研究了IzPack ,但找不到如何捆绑JRE(也许可以)。
那是一个相当小众的场景–通常我们是为部署到Linux服务器而开发的,但是有时可能需要使用Java为大型组织提供本地工具。 就我而言,长期运行的部分是预定的执行程序,但它也可以运行提供Web界面的码头服务。 在访问本地计算机很重要的情况下,为什么要这样做而不是提供URL。 它甚至可以是分布式搜索引擎( 例如 )或您要用Java编写的其他p2p软件。
翻译自: https://www.javacodegeeks.com/2016/06/installing-java-application-windows-service.html