tags:

views:

1092

answers:

6

Is there any way to reboot the JVM? As in don't actually exit, but close and reload all classes, and run main from the top?

A: 

AFAIK there is no such way.

Notice that if there were a way to do that, it would highly depend on the current loaded code to properly release all held resources in order to provide a graceful restart (think about files, socket/tcp/http/database connections, threads, etc).

Some applications, like Jboss AS, capture Ctrl+C on the console and provide a graceful shutdown, closing all resources, but this is application-specific code and not a JVM feature.

Miguel Ping
+3  A: 

IBM's JVM has a feature called "resettable" which allows you to effectively do what you are asking.

http://publib.boulder.ibm.com/infocenter/cicsts/v3r1/index.jsp?topic=/com.ibm.cics.ts31.doc/dfhpj/topics/dfhpje9.htm

Other than the IBM JVM, I don't think it is possible.

Dustin
+1  A: 

If you're working in an application server, they typically come with built-in hot deployment mechanisms that'll reload all classes in your application (web app, enterprise app) when you redeploy it.

Otherwise, you'll have to look into commercial solutions. Java Rebel (http://www.zeroturnaround.com/javarebel/) is one such option.

Jack Leow
+6  A: 

Your best bet is probably to run the java interpreter within a loop, and just exit. For example:

#!/bin/sh
while true
do
    java MainClass
done

If you want the ability to reboot or shutdown entirely, you could test the exit status:

#!/bin/sh
STATUS=0
while [ $STATUS -eq 0 ]
do
    java MainClass
    STATUS=$?
done

Within the java program, you can use System.exit(0) to indicate that you want to "reboot," and System.exit(1) to indicate that you want to stop and stay stopped.

Glomek
A: 

I do something similar using JMX, I will 'unload' a module using JMX and then 'reload' it. Behind the scenes I am sure they are using a different class loader.

Javamann
A: 

Not a real "reboot" but:

You can build your own class loader and load all your classes (except a bootstrap) with it. Then, when you want to "reboot", make sure you do the following:

  1. End any threads that you've opened and are using your classes.
  2. Dispose any Window / Dialog / Applet you've created (UI application).
  3. Close / dispose any other GC root / OS resources hungry peered resource (database connections, etc).
  4. Throw away your customized class loader, create another instance of it and reload all the classes. You can probably optimize this step by pre-processing the classes from files so you won't have to access the codebase again.
  5. Call your main point of entry.

This procedure is used (to some extent) while "hot-swapping" webapps in web servers.

Note though, static class members and JVM "global" objects (ones that are accessed by a GC root that isn't under your control) will stay. For example, Locale.setLocale() effects a static member on Locale. Since the Locale class is loaded by the system class loader, it will not be "restarted". That means that the old Locale object that was used in Locale.setLocale() will be available afterward if not explicitly cleaned.

Yet another route to take is instrumentation of classes. However, since I know little of it, I'm hesitant to offer advice.

Explanation about hot deploy with some examples

Ran Biron