windows开机自启动jar包的三种方式

方式一、利用组策略,后台静默运行


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快捷方式放进去即可

总结:

实际使用过程中,比较推荐部署成服务的方式,易于维护和管理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值