查了下资料,网上有许多方式都可以将jar以windows服务方式运行起来。当然前提是jar真的可以提供服务。
一、JavaService: 下载地址 http://download.forge.ow2.org/javaservice/JavaService_2_0_10-amd64.zip (WIN 64位)
http://download.forge.ow2.org/javaservice/JavaService-2.0.10.zip (WIN 32位)
JavaService 是一款能把 Java 应用程序做成 NT 服务的实用工具, 一般用于启动应用服务器.
二、nssm工具(可能是最简单的方法):
1.去nssm官网下载你nssm工具
2.解压文件,选择64/32位程序打开,如下图示:
界面直接明了,一般输入这两个参数即可,有其他需要可自己配置。没尝试,用到的可自行百度
1.在idea中用maven将程序打成jar,放到运行的目录中。
2.去github上面下载winsw: Releases · winsw/winsw · GitHubA wrapper executable that can run any executable as a Windows service, in a permissive license. - Releases · winsw/winswhttps://github.com/winsw/winsw/releases 2.1 如何查看.net版本
进入 C:\Windows\Microsoft.NET\Framework
2.2 下载WinSW
q
本人用的2.11版本
以下以本人所建目录和服务为例
在C:\下新建jaApp文件夹
将WinSW.NET4.exe改名为JaApp.exe
将sample-minimal.xml改名为JaApp.xml
将自己的springboot 用maven 打成 jar ,文件名为JaApp.jar
如下图
修改JaApp.xml内容,其中还有许多参数可以自行百度
<!--
MIT License
Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees,
Inc., 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/master/examples/sample-allOptions.xml
-->
<service>
<!-- ID of the service. It should be unique across the Windows system-->
<id>JaApp</id>
<!-- Display name of the service -->
<name>JaApp</name>
<!-- Service description -->
<description>Service for ja app</description>
<!-- java环境变量 -->
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<!-- Path to the executable, which should be started -->
<executable>java</executable>
<!--Xmx1024m 代表堆内存最大值为256MB -jar后面的是项目名-->
<arguments>-Xmx1024m -jar %BASE%\JaApp.jar</arguments>
<!-- 日志地址 -->
<logpath>%BASE%\log</logpath>
<!-- 日志模式 -->
<logmode>rotate</logmode>
</service>
新建 installJaApp.bat
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
JaApp.exe install
NET START JaApp
新建 uninstallJaApp.bat
NET STOP JaApp
sc delete JaApp
cd /d "%~dp0"
del /f /s /q ".\log\*.*"
rd /s /q ".\log"
以管理员方式运行
查看服务
可以停止服务,停止服务后才可以用 uninstallJaApp.bat 删除