将 JAVA 应用程序安装为 WINDOWS 服务

废话输出机器 2021-09-24 11:38:50 浏览数 (1709)
反馈

这听起来像是您永远不需要的东西,但有时,当您分发最终用户软件时,您可能需要安装 Java 程序作为 Windows 服务。

一个很少人拥有的重要先决条件——拥有一个捆绑的 JRE,所以没有人必须下载和安装一个JRE(会使安装过程不必要地复杂化,并且目标受众不一定精通技术)。

所以,用jar打包的maven项目,我首先想到的是打包一个exe(用​launch4j​),然后注册为服务。问题在于 java 程序使用预定的执行程序,因此它永远不会退出,这使得将其作为进程启动是不可能的。

所以我不得不使用​ ​commons-daemon​ ​procrun​对其进行"守护"。在此之前,我必须将所需的每个组件组装到一个目标文件夹中——​fat jar​(包括所有依赖项)、JRE、commons-daemon 二进制文件和配置文件。

相关位是(其中​${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 有 3 种模式: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
  • StartClass/StartMethod/StopClass/StopMethod 指向用于控制正在运行的应用程序的指定方法。在这种情况下,starting 只会调用 main 方法,stoping 会关闭调度的执行器,以便应用程序可以退出
  • classpath 参数指向fat jar
  • 使用 %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);
    }
}

然后我有一个“安装程序”文件夹,其中包含 jre 和 commons-daemon 文件夹以及两个 bat 文件和一个 fat jar。然后我可以将其打包为可自解压的存档并分发(当然还有手册)。我也查看了​IzPack​,但找不到如何捆绑 JRE(也许你可以)。这里的一个重要注意事项是您可能遇到的 32 位/64 位问题。这就是为什么捆绑 32 位 JRE 并使用 32 位(默认)prunsrv.exe 更安全的原因。

这是一个非常小众的场景——通常我们为部署到 Linux 服务器而开发,但有时可能需要为使用 Java 的大型组织提供本地工具。在我的例子中,长时间运行的部分是一个预定的执行器,但它也可以运行一个提供 Web 界面的码头服务。为什么要这样做,而不是提供 URL——在访问本地机器很重要的情况下。它甚至可以是一个分布式搜索引擎或另一个你想用 Java 编写的 p2p 软件。


0 人点赞