Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Threads and Synchronization and the fly likes Is there a thread that runs outside the jvm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is there a thread that runs outside the jvm" Watch "Is there a thread that runs outside the jvm" New topic
Author

Is there a thread that runs outside the jvm

Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
I am looking for a type of thread that does not need the JVM to run, such as if I start task manager and I end the javaw.exe process, my program should still run...

Are there any threads like that in java?

--thanks
devasia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Originally posted by Devasia Manuel:
I am looking for a type of thread that does not need the JVM to run, such as if I start task manager and I end the javaw.exe process, my program should still run...

Are there any threads like that in java?

--thanks
devasia


Well, once the JVM exits, all threads started by the JVM will be terminated also. So, when you say "does not need the JVM", you are talking about an external process, which you can start with a Runtime.exec() call.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Im not quite sure what you mean, could you please give me an example??
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Devasia Manuel:
Im not quite sure what you mean, could you please give me an example??


Threads always run inside a process.
Any java code always needs a jvm i.e. without a JVM you can not run any java code.
So, if you want something to run without a jvm, it can not be a java code but an external non-java process. This is what Herny meant i.e. you need to start an external process that will continue to run even after the jvm exits.
For example, if you have a shell script that keeps on polling a directory to look for new files. You want this script to start from a java program but still want it to continue after your java program exits.
Then, you can start this script using Runtime.exec(). Irrespective of whether the java code that started this script is alive or not this script will continue to run.

In a nutshell,

There is no java thread that can run outside jvm but there can be a non-java thread that runs outside the jvm. You can start such threads using Runtime.exec()


apigee, a better way to API!
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
ok, i think i get it thanks!!
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

What you want is the task manager for the OS. You can program it to start your application at times you choose. So if you have something that needs to be done at 2am every tuesday, you need to ask the OS to start your program at that time. Windows and Linux both have facilities for this.
Devasia Manuel
Ranch Hand

Joined: May 13, 2008
Posts: 57
Im not looking for the Runtime.exec() method since that only starts processes, I am looking for a way to execute a .jar file after the JVM has been shut down and that .jar file should still be running
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
As was pointed out above, a JVM is needed to execute a jar file. No JVM means no active Java process.

You can use Runtime.exec to start a second JVM that executes the jar file. Once that's up and running you can exit the first JVM. I think that would not terminate the second JVM; that's easy to find out, though.
[ September 27, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

When the user ends precess of javaw.exe your jar can not run anymore. There is no way a jar could run out of JVM.

Regards
Ananth Chellathurai
Sysvine Technologies


Ananth Chellathurai [Walk on software]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is there a thread that runs outside the jvm