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..
3 Answers
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/MyProgramin 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.
4 Comments
sachin11
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??
Ved
You need to set classpath/path variables in your system.
@blue add this in your answer too ulrichpalha.com/myblog/2011/11/19/…
robin
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
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.
1 Comment
Incerteza
doesn't while(true) load a processor 100%?
first create you jar bash and then add it to you crontab task list.
1 Comment
Anubhav Sharma
crontab is for scheduling, not for auto-start