自启有很多中方式,其中最好维护的一种方式是把启动命令注册成服务
- 首先去 https://github.com/kohsuke/winsw/releases 下载WinSW软件
- 我这里找的2.x的版本,3.x的版本是压缩包,下载这个exe和xml文件
3. 下载下来以后把文件放到jar包同目录下,并改为同名,像这样(这里的日志文件是我服务启动之后自动生成的)
4. 下面我们修改一下xml文件的配置
<service>
<!-- ID of the service. It should be unique accross the Windows system-->
<!--服务ID:启动、关闭、删除服务时,都是通过ID来操作的-->
<id>project-materials</id>
<!-- Display name of the service -->
<name>project-materials</name>
<!-- Service description -->
<description>project-materials</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 project-materials.jar -Xms512M -Xmx512M</arguments>
<!--
OPTION: onfailure
Defines 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: resetfailure
Time, after which the Windows service resets the failure status.
Default value: 1 day
-->
<resetfailure>1 hour</resetfailure>
<!--
OPTION: priority
Desired process priority.
Possible values: Normal, Idle, High, RealTime, BelowNormal, AboveNormal
Default value: Normal
-->
<priority>Normal</priority>
<!--
OPTION: stoptimeout
Time to wait for the service to gracefully shutdown the executable before we forcibly kill it
Default value: 15 seconds
-->
<stoptimeout>15 sec</stoptimeout>
<!--
OPTION: stopparentprocessfirst
If set, WinSW will terminate the parent process before stopping the children.
Default value: false
-->
<stopparentprocessfirst>false</stopparentprocessfirst>
<!--
OPTION: startmode
Defines 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>
-
下面就可以注册服务了
使用cmd打开命令端口
3.1 xxx.exe install
3.2 net start xxx (这个xxx是你在xml文件中配置的服务名)
-
net stop xxx 关闭服务
-
sc delete xxx 删除服务
使用windows启动文件夹自启程序
首先建立shell命令文件,文件后缀vbs
// 程序文件路径写自己的
createobject("wscript.shell").run"D:\重大危险源研判\后台\启动脚本.bat",0
然后建立快捷方式,将它放入windows启动文件夹里
windows启动文件夹查找方式:
按Win+R键打开运行窗口,输入“shell:Common Startup”代码