Winsw將jar包部署為windows服務


下載winsw

https://github.com/winsw/winsw/releases

下載winsw官網上的xml文件和.exe文件

 

 

 

創建文件編輯xml

創建一個文件夾,將所需要轉換的jar包,和這兩個文件放在一起,並且將.exe的文件名修改和.xml文件一樣

 

 xml的可選項

<!--
 這是Windows服務包裝器的一個配置示例。
 此配置文件應放在WinSW.exe同目錄,名稱應相同。
 例如,對於myapp.exe,配置文件名應為myapp.xml。
 更多信息:https://github.com/kohsuke/winsw/blob/master/doc/xmlconfigfile.md
-->
<configuration>
  
<!-- 
SECTION: 必填項
-->
  
  <!-- 服務ID,在windows系統中必須唯一 -->
  <id>myapp</id>
  <!-- 服務顯示名稱,只能英文和數字 -->
  <name>MyApp Service (powered by WinSW)</name>
  <!-- 服務描述,只能英文和數字 -->
  <description>This service is a service cratead from a sample configuration</description>
  
  <!-- 啟動可執行文件的路徑 -->
  <executable>%BASE%\myExecutable.exe</executable>
 
<!--
SECTION: 安裝
這些選項僅在安裝時有效。修改配置后,必須重新安裝服務才能生效。
-->
 
  <!--
    OPTION: serviceaccount
    定義運行服務的賬號。
  -->
  <!--
  <serviceaccount>
    <domain>YOURDOMAIN</domain>
    <user>useraccount</user>
    <password>Pa55w0rd</password>
    <allowservicelogon>true</allowservicelogon>
  </serviceaccount>
  -->
  
  <!--
    OPTION: onfailure
    定義一系列操作,如果托管的可執行文件失敗,則順序執行這些操作。
    支持的操作:restart,reboot,none
  -->
  <!--
  <onfailure action="restart" delay="10 sec"/>
  <onfailure action="restart" delay="20 sec"/>
  <onfailure action="reboot" />
  -->
  
  <!--
    OPTION: resetfailure
    windows服務重置故障狀態的時間。
    默認值 1 day
  -->
  <!--
  <resetfailure>1 hour</resetfailure>
  -->
 
<!--
SECTION: 執行管理配置
-->
 
  <!-- 
    OPTION: arguments
    傳遞給可執行文件的參數。
  -->
  <!--
  <arguments>-classpath c:\cygwin\home\kohsuke\ws\hello-world\out\production\hello-world test.Main</arguments>
  -->
 
  <!-- 
    OPTION: startarguments
    可執行文件啟動參數。
    如果配置,則覆蓋"arguments"
  -->
  <!--
  <startarguments></startarguments>
  -->
  
  <!--
    OPTION: workingdirectory
    如果指定,則設置可執行文件的默認工作目錄。
    默認值:服務包裝的可執行文件的目錄
  -->
  <!--
  <workingdirectory>C:\myApp\work</workingdirectory>
-->
  
  <!--
    OPTION: priority
    所需的進程優先級。
    可設置的值:Normal, Idle, High, RealTime, BelowNormal, AboveNormal
    默認值:Normal
  -->
  <priority>Normal</priority>
  
  <!-- 
    OPTION: stoptimeout
    強制終止可執行文件之前,嘗試正常關閉的等待時間。
    默認值:15 seconds
  -->
  <stoptimeout>15 sec</stoptimeout>
    
  <!--
    OPTION: stopparentprocessfirst
    如果設置為true,在停止子進程之前終止父進程。
    默認值:false
  -->
  <stopparentprocessfirst>false</stopparentprocessfirst>
 
  <!-- 
    OPTION: stopexecutable
    執行關閉服務的可執行文件路徑。
    只有配置了"stoparguments"時,才使用此配置。如果沒有則用"executable"關閉服務
  -->
  <!--
  <stopexecutable>%BASE%\stop.exe</stopexecutable>
  -->
 
  <!-- 
    OPTION: stoparguments
    終止服務時,傳遞給"stopexecutable"的附加參數
    此選項還允許通過停止可執行文件終止可執行文件
  -->
  <!--
  <stoparguments>-stop true</stoparguments>
  -->
<!-- 
SECTION: 服務管理
-->
    <!--
      OPTION: startmode
      定義服務的啟動模式
      支持的模式:Automatic, Manual, Boot, System (latter ones are supported for driver services only)
      默認模式:Automatic
    -->
    <startmode>Automatic</startmode>
    
    <!--
      OPTION: delayedAutoStart
      如果在"startmode"配置了"Automatic",則啟用延遲自動啟動。
      請參閱Winsw文檔以獲取有關支持的平台版本和限制的信息。
    -->
    <!--
        <delayedAutoStart/>
    -->
    
    <!-- 
      OPTION: depend
      在本服務啟動之前啟動的服務。
    -->
    <!--
    <depend>Eventlog</depend>
    <depend>W32Time</depend>
    -->
    
    <!--
      OPTION: waithint
      掛起停止操作所需的時間
      在指定的時間段過去之前,服務應該下次調用setServiceStatus函數。
      否則服務將被標記為無響應
      默認值:15 seconds
    -->
    <waithint>15 sec</waithint>
    
    <!--
      OPTION: sleeptime
      服務下次調用setServiceStatus函數之前的時間。
      不要超過"waithint"。建議間隔是"waithint"的十分之一,但不小於1秒,也不大於10秒。
      默認值:1 second
    -->
    <sleeptime>1 sec</sleeptime>
    
    <!--
      OPTION: interactive
      指示服務可以與桌面交互。
    -->
    <!--
    <interactive/>
    -->
    
<!-- 
SECTION: 日志記錄 
-->
 
  <!--
    OPTION: logpath
    為服務包裝程序生成的所有日志設置自定義日志目錄。
    默認值:包含執行器的目錄
  -->
  <!--
    <logpath>%BASE%\logs</logpath>
  -->
  
  <!--
    OPTION: log
    為可執行文件生成的日志定義日志記錄模式。
    支持的模式:
      * append - 追加到現有日志
      * none - 不保存日志
      * reset - 啟動時擦除日志
      * roll - 根據大小自動循環分割日志
      * roll-by-time - 根據日期循環分割日志
    默認模式: append
    
    每種模式都有不同的設置。https://github.com/kohsuke/winsw/blob/master/doc/loggingAndErrorReporting.md
  -->
  <log mode="append">
    <!--
    <setting1/>
    <setting2/>
  -->
  </log>
  
<!--
SECTION: 環境設置
-->
  <!--
    OPTION: env
    設置或重寫環境變量。
    頂層可能配置了多個條目。
  -->
  <!--
  <env name="MY_TOOL_HOME" value="C:\etc\tools\myTool" />
  <env name="LM_LICENSE_FILE" value="host1;host2" />
  -->
 
 
  <!--
    OPTION: download
    啟動前由包裝器執行的下載列表。
  -->
  <!--
  <download from="http://www.google.com/" to="%BASE%\index.html" />
  
  下載並在出現錯誤時使服務啟動失敗:
  <download from="http://www.nosuchhostexists.com/" to="%BASE%\dummy.html" failOnError="true"/>
  由於連接未加密而導致基本身份驗證不安全的示例:
  <download from="http://example.com/some.dat" to="%BASE%\some.dat"
            auth="basic" unsecureAuth=“true”
            username="aUser" password=“aPassw0rd" />
  通過HTTPS安全基本身份驗證
  <download from="https://example.com/some.dat" to="%BASE%\some.dat"
            auth="basic" username="aUser" password="aPassw0rd" />
  當目標服務器和客戶端是同一域的成員,並且客戶端域屬於具有信任的域時,安全身份驗證:
  <download from="https://example.com/some.dat" to="%BASE%\some.dat" auth="sspi" />
  -->
 
<!-- 
SECTION: 其他選項
-->
  
  <!--
    OPTION: beeponshutdown
    指示服務在關閉時(如果操作系統支持)應發出嘟嘟聲。
  -->
  <!--
  <beeponshutdown/> 
  -->
  
<!--
SECTION: 擴展
此配置部分允許指定自定義擴展。
更多信息請訪問:https://github.com/kohsuke/winsw/blob/master/doc/extensions/extensions.md
-->
 
<!--
<extensions>
  Extension 1: id values must be unique
  <extension enabled="true" id="extension1" className="winsw.Plugins.SharedDirectoryMapper.SharedDirectoryMapper">
    <mapping>
      <map enabled="false" label="N:" uncpath="\\UNC"/>
      <map enabled="false" label="M:" uncpath="\\UNC2"/>
    </mapping>
  </extension>
  ...
</extensions>
-->
 
</configuration>

示例xml

<service>
<id>test</id>
<name>test</name>
<description>test</description>
<executable>E:\jdk\bin\java.exe</executable>
<arguments>-jar testjar.jar</arguments>
<startmode>Automatic</startmode>
<logpath>%BASE%/log</logpath>
<logmode>rotate</logmode>
</service>

運行exe

 

 

刪除服務

以管理員命令運行

sc  delete  test

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM