aspose file tools*
The moose likes Beginning Java and the fly likes How to make a java program run always Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to make a java program run always" Watch "How to make a java program run always" New topic
Author

How to make a java program run always

Karthikean Konangi
Greenhorn

Joined: Jan 31, 2008
Posts: 22
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.


Regards,
KonangiK
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14349
    
  22

Just create a loop in your application that never stops, and don't return from the main() method. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Not really an intermediate problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Karthikean Konangi
Greenhorn

Joined: Jan 31, 2008
Posts: 22
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

Joined: Nov 02, 2007
Posts: 63
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 ]

Technology can never substitute for knowledge.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14349
    
  22

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

Joined: Jun 25, 2008
Posts: 2
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

Joined: Jan 31, 2008
Posts: 22
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ..


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
vaibhav gupta
Greenhorn

Joined: Jun 25, 2008
Posts: 2
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

Joined: May 01, 2008
Posts: 43
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


Regards
SaurabhSri (SCJP 1.5)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39851
    
  28
"Vaibhav g" please read your private messages again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to make a java program run always