Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to "daemonize" a Java program

Status
Not open for further replies.

Clairvoyant1332

Programmer
May 21, 2003
147
US
I'm trying to find a way to have a java program put itself in the background, rather than having the shell do it via "nohup java class" or "java class &". I tried creating a sample program that calls setDaemon() as follows:

Code:
public class jthread extends Thread
{
  public void run()
  {
    int i;

    try { sleep(1000); } catch (InterruptedException e) {}
    for (i=0;i<10;i++) {
      System.out.println("i="+i);
      try { sleep(2000); } catch (InterruptedException e) {}
    }
  }

  public static void main(String[] args)
  {
    jthread j= new jthread();
    j.setDaemon(true);
    j.start();
    try{ sleep(2500); } catch (InterruptedException e) {}
  }
}

I would think that the thread would continue to run after main exits, but that doesn't appear to be the case.

Is there a way to do this, and if so how?
 
It doesn't work like that - to have a process run in the background means the process itself must fork - a therad is just running in that process, a thread is not actually a process. So you need to tell the java programmme itself to fork.

This is achievable if you wrote a native app that forked a java process, but not within "java" itself.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I kinda figured as much. Thanks for the connfirmation.

That said, where would a daemon thread typically be used? Is it just a way to avoid having to wait for certain threads to exit?
 
From the API docs :

The Java Virtual Machine exits when the only threads running are all daemon threads.

So it means the JVM will exit if setDaemon(true) is applied - but of course, this does not actually mean the threads will keep running on the system - because they run within the process (ie the JVM) - bye bye JVM == bye byte threads.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I think the OP is on linux Dian ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Dunno what is a "OP" but that product also works on UNIX. I know GNU is not Unix, but should be a workaround too.

Cheers,
Dian
 
OP == Original Poster.

But that link is entitled "Running Java Applications as a Windows Service" .

In any case, why would you want to bother going through all that faff on Linux when you can just run the java process in the background by doing :

java Bla &

?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Well, yes, but it points to an open source extension that works either under Windows and Unix.

And about the reason to do that, I have no idea, ask the "OP" :p

Anyway, I guess that if the software exists it's because someone bothered to do it, so will have any use case I can't think of.

Cheers,
Dian
 
The idea was to have something that works under either Unix or Windows, which is why I was looking for something within Java to do this.

We do have some java apps on production Solaris machines that essentially run "java class &" from a script. However, if an SA needs to manually restart the java app, and his shell is /bin/sh, the java app exits when the shell quits. Granted, this can be fixed by using the nohup command or changing the script's shell...
 
Well thats the rub isn't it - Java is meant to be "platform independant" - but in reality, we all know it just isn't - so you end up hacking around, writing different scripts for different OS's, or even as I suggested earlier, writing native C code to spawn processes which can fork for each OS. If only the java executable had a "fork" switch eh !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top