• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make a java program run always

 
Karthikean Konangi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have created a Java program which need to run always. it has to keep on running. How can i create that . Please advise.

Thanks in advance.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just create a loop in your application that never stops, and don't return from the main() method. For example:
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really an intermediate problem.
 
Karthikean Konangi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies.
I guess, i didnot ask the question properly.

My question is " I already wrote a Java program with infinate loop.But how can i make this job as a cron/ scheduled job / some job so that, it has to run always in the server. I should be able to start/stop the process and close the command prompt. How can i do this??
 
Christopher Young
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess, if you're on Windows, anyway, set it [Windows] up to launch the application you need at start, or, launch a batch file that launches the application you need.

Is there a way to do this in the Java library? I don't know, as I hear it tries not to be OS-specific.

I suppose you could use the Java Native Interface, maybe. Or maybe I'm off track.
[ June 25, 2008: Message edited by: Christopher Young ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to run Java programs as Windows services, that start when Windows starts. Search for "java windows service" on Google.

On Unix or Linux, you can add a startup script somewhere in the /etc folder to start your program at boot. How this works exactly depends on what flavour of Unix or Linux you're using (read the documentation of your OS).
 
vaibhav gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using java.util.TimerTask

A task that can be scheduled for one-time or repeated execution by a Timer.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/TimerTask.html

Here is a sample code:;Hope this helps..

-Vaibhav

[edit]Add code tags. CR[/edit]
[ June 25, 2008: Message edited by: Campbell Ritchie ]
 
Karthikean Konangi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Again I have to intiate TestScheduler process and, if I close the Command Prompt the process will close automatically.

And my OS is Sun Solaris 5.8


Please advise.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
search for "Quartz job Scheduler" for job scheduling . and make a simple java program which takes START OR STOP command , which in turn start/stop your job class ..
 
vaibhav gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use invoke the process in background (as you said you are using solaris os)

See: http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch12s02.html

e.g.

java TestScheduler &

After this you can even close the command prompt.
 
SaurabhSri Sri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can run a java program as a daemon process in UNIX environment by using Java Service Wrapper. Please see this url -
Java Service Wrapper
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Vaibhav g" please read your private messages again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic