We help IT Professionals succeed at work.
Get Started

Java Class loading problem

msk_apk
msk_apk asked
on
1,226 Views
Last Modified: 2012-05-11
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

Comment
Watch Question
Awarded 2011
Awarded 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE