App下載

將 JAVA 應用程序安裝為 WINDOWS 服務

廢話輸出機器 2021-09-24 11:38:50 瀏覽數(shù) (2539)
反饋

這聽起來像是您永遠不需要的東西,但有時,當您分發(fā)最終用戶軟件時,您可能需要安裝 Java 程序作為 Windows 服務。

一個很少人擁有的重要先決條件——擁有一個捆綁的 JRE,所以沒有人必須下載和安裝一個JRE(會使安裝過程不必要地復雜化,并且目標受眾不一定精通技術)。

所以,用jar打包的maven項目,我首先想到的是打包一個exe(用?launch4j?),然后注冊為服務。問題在于 java 程序使用預定的執(zhí)行程序,因此它永遠不會退出,這使得將其作為進程啟動是不可能的。

所以我不得不使用? ?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 管理服務的文件。安裝程序創(chuàng)建服務。Commons-daemon 有 3 種模式:exe(允許你包裝任意可執(zhí)行文件)、Java(類似于 exe,但用于 java 應用程序)和 jvm(在同一進程中運行 java 應用程序)。

我可以使用所有三個選項(包括 launch4j 創(chuàng)建的 exe),但是 jvm 允許您使用指定的方法來控制正在運行的應用程序。StartClass/StartMethod/StopClass/StopMethod 參數(shù)就是為了這個。這是整個 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參數(shù)指向jvm dll
  • StartClass/StartMethod/StopClass/StopMethod 指向用于控制正在運行的應用程序的指定方法。在這種情況下,starting 只會調用 main 方法,stoping 會關閉調度的執(zhí)行器,以便應用程序可以退出
  • classpath 參數(shù)指向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。然后我可以將其打包為可自解壓的存檔并分發(fā)(當然還有手冊)。我也查看了?IzPack?,但找不到如何捆綁 JRE(也許你可以)。這里的一個重要注意事項是您可能遇到的 32 位/64 位問題。這就是為什么捆綁 32 位 JRE 并使用 32 位(默認)prunsrv.exe 更安全的原因。

這是一個非常小眾的場景——通常我們?yōu)椴渴鸬?Linux 服務器而開發(fā),但有時可能需要為使用 Java 的大型組織提供本地工具。在我的例子中,長時間運行的部分是一個預定的執(zhí)行器,但它也可以運行一個提供 Web 界面的碼頭服務。為什么要這樣做,而不是提供 URL——在訪問本地機器很重要的情況下。它甚至可以是一個分布式搜索引擎或另一個你想用 Java 編寫的 p2p 軟件。


0 人點贊