I have a simple java program which is just a single piece of code that reads from a database and modifies the contents of the database based on certain conditions. Now, what I want is that this program should start automatically at the startup and silently run in the background unless someone kills it from the task manager. I have never done something like this before and don't know exactly how to go about it. Can someone help me out as to how this can be done? Thank you..
Asked
Active
Viewed 3.4k times
10
-
1You want your java application to run on startup? Why not put a bat file on the startup folder to start the java applicaiotn? That should get it done assuming it's windows... – Thihara May 05 '12 at 06:17
3 Answers
9
Follow the these steps to do the job :(Assuming you are using windows and jre is installed )
- First compile your java program and place the class file at one location.
- Now create a bat file and place
java LOCATION TO THAT CLASS FILE/MyProgram
in that. - Put your bat file in start up programs
- Restart the system, you will get your program running in back ground..! Hope this will help you.

Ved
- 8,577
- 7
- 36
- 69
-
Thnx for the suggestion. But I am running into a bit of problem when I am giving the absolute path to the class file in the command. Its throwing this error "Exception in thread "main" java.lang.NoClassDefFoundError: C:\Documents and Settings\Desktop\Test". However if I migrate to the directory and then run the command, it works fine! Any insight into this?? – sachin11 May 07 '12 at 07:03
-
-
@blue add this in your answer too http://ulrichpalha.com/myblog/2011/11/19/using-commons-daemon-procrun-to-run-your-java-application-as-a-windows-service/ – Dec 14 '12 at 13:08
-
It's been a long time since this question has been asked, but in case anyone is reading this later on, writing your batch and then creating a Windows link (right click>Create Link) and copying the link in the startup folder avoids the classpath problems – robin Oct 18 '20 at 09:10
1
There are two problems here
How to add this program to the startup
Run the program as a daemon (background process)
- Simplest way to do is using a while loop and sleep for required time interval in the while loop. Then perform the database operation.
Also for windows, you can check this JSL http://www.roeschter.com/
Thanks.

Community
- 1
- 1

Manish Mulani
- 7,125
- 9
- 43
- 45
-3
first create you jar bash and then add it to you crontab task list.

Box Box Box Box
- 5,094
- 10
- 49
- 67

Mike
- 1
- 2