Run Java application at Windows startup

2019-01-03 09:29发布

I have a JAR file containing a Java application. How can I make it start with Windows, without needing user interaction?

8条回答
姐就是有狂的资本
2楼-- · 2019-01-03 10:00

If you want to do it programmatically from Java you can write directly into Windows registry startup folder.

Here is link how to write into Windows registry programmatically.

when you have implemented function to work with registry than what you need is just run this code

String value = "\"javaw -jar " + System.getProperty("user.dir") + "\\myJar.jar\"";
WinRegistry.writeStringValue(WinRegistry.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "myJar autorun key", value);

where value for key need to be command what runs your application like java -jar myJar.jar

to remove it from autorun you simply

WinRegistry.deleteValue(WinRegistry.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", "myJar autorun key");

UPDATE

Replace WinRegistry.writeStringValue with WinRegistry.setStringValue recent version of java 1.8.x
查看更多
再贱就再见
3楼-- · 2019-01-03 10:02

Haha...easy! from run(u can press start+r) write regedit then: HKey local machine->software->microsoft->windows->current version -> run click on it and in the other panel right-click on nothing and choose add -> string value name it java double click it and put it's value as follow: 'javaw -Xmx200m -jar C:\Path\to\jarfile\TheJar.jar' Hope that I could help you ^_^

查看更多
爷、活的狠高调
4楼-- · 2019-01-03 10:03

it's simple as you have to put shortcut in

Windows 7 C:\users\All Users\Start Menu\Programs\Startup(Admin) or User home directory(%userProfile%)

Windows 10 : In Run shell:startup

in it's property -> shortcut -> target - > java.exe -jar D:\..\runJar.jar

NOTE: This will run only after you login


With Admin Right

sc create serviceName binpath= "java.exe -jar D:\..\runJar.jar" Will create windows service

if you get timeout use cmd /c D:\JAVA7~1\jdk1.7.0_51\bin\java.exe -jar d:\jenkins\jenkins.war but even with this you'll get timeout but in background java.exe will be started. Check in task manager


In some restricted environment as I was in corporate environment

ERROR:

The service did not respond to the start or control request in a timely fashion

In this case

cmd /c D:\JAVA7~1\jdk1.7.0_51\bin\java.exe -jar d:\jenkins\jenkins.war

This will give you an error if you run manually but will run in background.

NOTE: This will run at windows logon start-up(before sign-in, Based on service 'Startup Type')

Detailed explanation of creating windows service


Regedit

Note: Edit Advanced User only

To Run for Current User Only

HKEY_CURRENT_USER/SOFTWARE/MICROSOFT/WINDOWS/CURRENT_VERSION/RUN

To Run for All Users

hkey_local_machine/SOFTWARE/MICROSOFT/WINDOWS/CURRENT_VERSION/RUN

Create a String with Name and Path using above command

查看更多
聊天终结者
5楼-- · 2019-01-03 10:08

Use "winsw" - http://kenai.com/projects/winsw - which was written for Glassfish v3 but works well with Java programs in general.

Require .NET runtime installed.

查看更多
祖国的老花朵
6楼-- · 2019-01-03 10:11

If you are not ready to do the config yourself or if you want the same functionality on multile computers, then you can use Advanced Installer. You can package jars to be installed on Windows and set params that will run your program on startup

查看更多
Evening l夕情丶
7楼-- · 2019-01-03 10:14

In order to create service from any executable use srvany.exe from Windows Resource Kits 2003 (take attention to spaces after =)::

cmd> sc create NAME binPath= "c:\Program Files\Windows Resource Kits\Tools\srvany.exe" ^
   type= own start= auto error= normal DisplayName= "NAME for services.msc"

Then pass what srvany.exe wrapper will do:

cmd> reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NAME\Parameters" ^
     /v "Application" ^
     /d "\"c:\Program Files\Java\jre7\bin\java.exe\" -cp c:\home\devel\service Main"

Above you see quoting syntax for spaces. Next start service with:

cmd> sc start NAME

If you make error recheck your settings with:

cmd> reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NAME" /s

and remove service:

cmd> sc delete NAME

and make steps again.

Visit GUI services.msc and check with procexp.exe service actually start.

See also: creating a service with sc.exe; how to pass in context parameters

NOTE All involved instruments is official Microsoft!!!

查看更多
登录 后发表回答