方式一、利用组策略,后台静默运行
1.编写运行运行jar包脚本的脚本,如
@echo off
start /d "D:\visitor-services\" startup.bat
@echo on
2.
Windows+R运行,输入gpedit.msc进入组策略编辑器,选中windows设置-启动,然后点击添加脚本即可。
方式二、利用服务(强烈推荐)
1.下载winsw.exe 和.xml(注意:可能需要安装 .net framework4)
地址:https://github.com/winsw/winsw/releases
2.与jar包同路径,同名,改名.exe与.xml并配置 xxx.xml
配置说明 :https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
我的配置:
<service><!-- ID of the service. It should be unique accross the Windows system--><!--服务ID:启动、关闭、删除服务时,都是通过ID来操作的--><id>file-service</id><!-- Display name of the service --><name>file-service</name><!-- Service description --><description>fileservice</description><!-- Path to the executable, which should be started --><!--当前电脑配置了java环境变量,直接写成“java”就行;你也可以写成类似这样:D:\develop\jdk1.8\jre\bin\java--><executable>java</executable><!--<arguments>-jar "C:\Users\Administrator\Desktop\winsw\statement-0.0.1-SNAPSHOT.jar"</arguments>--><arguments>-jar file-service.jar -Xms512M -Xmx512M</arguments><!--OPTION: onfailureDefines a sequence of actions, which should be performed if the managed executable fails.Supported actions: restart, reboot, none--><onfailure action="restart" delay="10 sec"/><onfailure action="restart" delay="20 sec"/><!--OPTION: resetfailureTime, after which the Windows service resets the failure status.Default value: 1 day--><resetfailure>1 hour</resetfailure><!--OPTION: priorityDesired process priority.Possible values: Normal, Idle, High, RealTime, BelowNormal, AboveNormalDefault value: Normal--><priority>Normal</priority><!-- OPTION: stoptimeoutTime to wait for the service to gracefully shutdown the executable before we forcibly kill itDefault value: 15 seconds--><stoptimeout>15 sec</stoptimeout><!--OPTION: stopparentprocessfirstIf set, WinSW will terminate the parent process before stopping the children.Default value: false--><stopparentprocessfirst>false</stopparentprocessfirst><!--OPTION: startmodeDefines start mode of the service.Supported modes: Automatic, Manual, Boot, System (latter ones are supported for driver services only)Default mode: Automatic--><startmode>Automatic</startmode><waithint>15 sec</waithint><sleeptime>1 sec</sleeptime><log mode="roll-by-size"><sizeThreshold>10240</sizeThreshold><keepFiles>8</keepFiles>
</log>
</service>
3.cmd命令行,输入 xxx.exe install 安装服务
如果权限不足,需要进入c:\windows\System32 目录下,用管理员权限运行cmd
4.net start xxx 启动服务
net stop xxx 关闭服务
sc delete xxx 删除服务
方式三、添加到启动项,前台运行
把bat脚本,或者.exe快捷方式放进去即可
总结:
实际使用过程中,比较推荐部署成服务的方式,易于维护和管理。