troubleshooting Question

Java Class loading problem

Avatar of msk_apk
msk_apkFlag for India asked on
Java
14 Comments1 Solution1228 ViewsLast Modified:
I am trying to reload a class dynamically as per the below code.

1. After the first print, I am modifying the code and recompiling the code with different print statments. say for example the new printSomething method prints "here" alone.
2. after the second loadClass() statement, i am expecting the new printSomething() method to get executed which does not happen.  It always prints "printing here" irrespective of how many times I load the class through loadClass() method.

Believe I am missing something here.

import java.io.*;
import java.net.*;
public class DClassLoader {
	
	public static void main(String a[]) throws Exception
	{
		ClassLoader sysLoader = DClassLoader.class.getClassLoader();
		Print l1 = (Print)sysLoader.loadClass("PrintObj").newInstance();
		l1.printSomething();

		Thread.sleep(30000);
		
		Print l2 = (Print)sysLoader.loadClass("PrintObj").newInstance();
		l2.printSomething();
		
	}	
}

interface Print
{
	public void printSomething();
}

class PrintObj implements Print
{
	public void printSomething()
	{
		System.out.println(" printing here");
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros