Java服务注册为Windows服务

当我们开发好一个Java应用时,再部署到服务器时,最简单的方法是直接java -jar xxx.jar 。

但是这样非常的不优雅,后台必须一直保留着一个控制台黑色窗口,很不爽,总可能被别人误关,而且最重要的是如果服务器重启了, 还要手动去再启动Java程序,很麻烦。

然而把Java程序注册成Windows服务就完美解决了这些问题。

步骤也很简单。

这是使用到的工具。

链接:https://pan.baidu.com/s/1L1JR5uABaYnEUTNHXFV5ZQ?pwd=h8uy 
提取码:h8uy

 

这里的supPlant-systemInfo其实就是我们要注册成的Windows服务的名字,大家要改成自己的,supPlant-systemInfo.exe和supPlant-systemInfo.xml名字要统一。

supPlant-systemInfo.exe里面不需要管。

主要的配置就在supPlant-systemInfo.xml里。

<!--
    Copyright (c) 2016 Oleg Nenashev and other contributors

    Permission is hereby granted, free of charge, to any person obtaining a copy of this 
    software and associated documentation files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or 
    substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<!--
 This is an example of a minimal Windows Service Wrapper configuration, which includes only mandatory options.
 
 This configuration file should be placed near the WinSW executable, the name should be the same.
 E.g. for myapp.exe the configuration file name should be myapp.xml
 
 You can find more information about the configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
 Full example: https://github.com/kohsuke/winsw/blob/sample-config-file/examples/allOptions.xml
-->
<configuration>
  
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>supPlant-systemInfo</id>
  <!-- Display name of the service -->
  <name>supPlant-systemInfo</name>
  <!-- Service description -->
  <description>系统信息分析服务</description>
  <!-- Path to the executable, which should be started -->
  <!-- jdk安装目录 -->
  <executable>D:\systemInfo\jdk1.8\bin\java</executable>
  <!-- 跟java -jar 的启动方式一样,可以自己配置一些参数,选择要启动的jar包 -->
  <arguments>-Xmx1500m -XX:MetaspaceSize=256m -XX:MaxPermSize=256m -jar -Dfile.encoding=UTF-8 -DOPERATING.tcp.port=5679 plugin-0.0.1-SNAPSHOT.jar</arguments>
<logmode>none</logmode>
  <!-- 开机自启动 -->
<startmode>Automatic</startmode>
</configuration>

代码中有具体的注释。

这就配置好了,再就是安装Windows服务 ,和卸载Windows服务。

下面是regsystemInfo.bat的代码,里面包含了安装和卸载。(这个文件尽量使用管理员权限运行)

@echo off
rem -----------------------------------------------------------------------------
rem install Script for supPlant-systemInfo
rem -----------------------------------------------------------------------------

cls
echo Welcome to Install supPlant-systemInfo!
echo.
set error=0
%~dp0\..\supPlant-systemInfo.exe uninstall
%~dp0\..\supPlant-systemInfo.exe install
echo.
pause

ok了, 这段代码是先卸载再安装,到这就完成了,看一下成果吧。

第一次需要手动启动。 

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值