Run Java application at Windows startup
Solution 1
Create a .bat file and put this inside:
javaw -Xmx200m -jar C:\Path\to\jarfile\TheJar.jar
Then put the .bat file into the windows startup folder.
One more thing: There's a difference between using java and javaw. While java is better when you are debugging an application, the application prints text or something like that, javaw is better when you don't need that. Why? Because java runs java program using a console that shows all that application prints (println's, exception stacktraces and so on) while javaw doesn't run on console.
Solution 2
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
Solution 3
The answer to this question might suit your needs. Setup your java application to run as a windows service and you should be good to go.
Solution 4
- Open 'Run' (Windows-Key+
R
) - Write
regedit
- Go to
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 follows:
javaw -Xmx200m -jar C:\Path\to\jarfile\TheJar.jar
Solution 5
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
Related videos on Youtube
shan
Updated on January 27, 2020Comments
-
shan over 4 years
I have a JAR file containing a Java application. How can I make it start with Windows, without needing user interaction?
-
Vadzim over 5 years
-
-
Hussain Akhtar Wahid 'Ghouri' over 10 yearsanyother way than keeping it inside start up folder
-
gavenkoa over 9 years
sc create
wouldn't work asjava.exe
isn't implementStartServiceCtrlDispatcher
. See msdn.microsoft.com/en-us/library/windows/desktop/… -
Daniel Kvist almost 9 years@ThorbjørnRavnAndersen When would it happend otherwise?
-
matbrgz almost 9 years@TheDDestroyer12 services are run at boot time (if configured so).
-
Daniel Kvist almost 9 years@ThorbjørnRavnAndersen Ah, sorry.
-
jechaviz almost 6 yearsdoes it needs the apostrophes at the begining and at the end? or quotes? or nothing?
-
user3379482 almost 6 yearsNo - You can leave it empty if the directory has no spaces
-
jechaviz almost 6 yearsJust I was thinking about the initial aphostrophe in your answer, i mean: --> 'javaw...
-
Seyed Ali Roshan almost 3 yearsis there any way we can see std logs after this? in Linux by systemd units u can always try and look at the logs by running journalctl, so is there any alternative to that with this method?