Click to See Complete Forum and Search --> : Just a theoretical question... Is java able to use .class runtime?


Silver Ghost
November 4th, 2002, 01:50 AM
What I am trying to say is. For example. I have some program (call it server). And I want it to load and use a specific .class file. But I also want it to make it so, that when I change the .java file and recompile it into that .class file, and tell my server to reload the .class file it will do it.

Is it possible with Java?

dlorde
November 4th, 2002, 05:10 AM
In principle, yes.

Silver Ghost
November 4th, 2002, 09:43 AM
WEEEEEEEEE!

:)

Can you provide me with a link or something where I can read more about it?

dlorde
November 5th, 2002, 03:07 PM
Sorry about the delay - I did reply earlier, but my post has just disappeared...

What you are asking is quite unusual - you will need to execute a command process to run javac to compile your class (and possibly jar to put it in a jar file). For this, see the Runtime (http://java.sun.com/j2se/1.4/docs/api/java/lang/Runtime.html) class and its exec() methods. You will also need to dynamically load and instantiate the modified class, for which you will need to use the Class (http://java.sun.com/j2se/1.4/docs/api/java/lang/Class.html) class, and possibly a custom ClassLoader (http://java.sun.com/j2se/1.4/docs/api/java/lang/ClassLoader.html).

I haven't tried reloading a modified class, but it may be problematic due to class loader problems. If so, you might find it easier to make the modifications as a subclass of the original and load that as a new class. The The JavaVirtual Machine Specification (http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#72007) give details on the loading of classes at runtime.

Good luck :D