loading classes
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sender_jones
Posted On:   Thursday, February 14, 2008 10:45 PM

I wrote a java program that runs forever. At certain time interval, it loads classes specified in a file and start a thread for each of these classes to execute. Here is my coding related to loading of the classes: Class[] cons = {String.class, Vector.class}; Constructor c = Class.forName(myClass).getConstructor(cons); Object[] obj = new Object[] {myString, myVector}; Object o = c.newInstance(obj); Thread t = new Thread((Runnable) o); t.start(); It works well. But when I change the coding of myClass and recompile, it didn't effect on the next execution. Instead, it still uses the old version. I checked the hashCode of o and found that it was different in e   More>>

I wrote a java program that runs forever. At certain time interval, it loads classes specified in a file and start a thread for each of these classes to execute. Here is my coding related to loading of the classes:

			
				
Class[] cons = {String.class, Vector.class};
Constructor c = Class.forName(myClass).getConstructor(cons);
Object[] obj = new Object[] {myString, myVector};
Object o = c.newInstance(obj);
Thread t = new Thread((Runnable) o);
t.start();

It works well. But when I change the coding of myClass and recompile, it didn't effect on the next execution. Instead, it still uses the old version. I checked the hashCode of o and found that it was different in each execution, which was expected. How can I make the new coding effect in next execution?    <<Less

Re: loading classes

Posted By:   Giacomo_Veneri  
Posted On:   Saturday, March 1, 2008 12:28 PM

You need to use a different context class loader

Re: loading classes

Posted By:   Robert_Lybarger  
Posted On:   Thursday, February 14, 2008 11:19 PM

So after starting up the virtual machine, which starts the default system class loader, which resolves and creates the initial MyClass class, you are later changing the associated MyClass.class file on the filesystem, and expecting the already-running VM to use the new code when you create another instance of MyClass? That'd be a bit of a security risk, so I doubt making that happen would be at all easy. Sounds like, if such a thing is possible, you'll have to monkey around with ClassLoaders or something. I've actually never played with this sort of thing, so its beyond my experience (other than what a search engine might help me dig up).
About | Sitemap | Contact