• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Related to dynamic loading of Class

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI! ALL...
I am facing a problem related to dynamic loading of class .
The class below (Continuous.class) has got a reference to class Afile
Which is being loaded dynamically through Class.forName() but after the first invocation to loadNewVersion() when Thread.sleep(20000) is called i change the Afile.java mean while but that change is not getting reflected in the second invocation during the single run of the program.

Continuous.java

public class Continuous
{
Afile afile=null;
public void loadNewVersion()
{

try{
afile=(Afile)Class.forName("Afile").newInstance();
System.out.println("new instance created");

}catch(ClassNotFoundException cnfe)
{
System.out.println("Afile not found");

}
catch(InstantiationException ie)
{
System.out.println("instantation exception ");
}
catch(IllegalAccessException iae)
{
System.out.println("illegal access exception ");

}
catch(MalformedURLException mue)
{
System.out.println("malformed exception ");
}


}


public static void main(String args[])
{
System.out.println("Inside main method");
Continuous obj=new Continuous();
obj.test();
try
{
Thread.sleep(20000);
}catch(InterruptedException iex)
{
System.out.println("Thred Interrupted");
}

obj.test();

}

public void test()
{
loadNewVersion();
System.out.println(afile.getTitle());

}






}


Afile.java

public class Afile
{

public String title;

public String getTitle()
{
return "this is a old one newwe ";
}


}
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once a class has been loaded by a particular ClassLoader, it will not be loaded again by the same ClassLoader in the same JVM.

If you really need to load a new version of a class in the same JVM, you must use a new ClassLoader.

This stuff is tricky to get right. What real need are you trying to fulfill here? Do you really need to reload a class?
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI! Peter ,
Even when the Class is changed still the ClassLoader won't load the newer version of class.Doesn't call to Class.forName() look for whether the class has changed or not before loading that class???

i came across an example on net related to URLClassLoader and that is working fine but i have to pick the classes from the CLASSPATH without mentioning any specific directory as they are using ..
http://java.sun.com/developer/TechTips/2000/tt1027.html

mine is a reporting tool which queues reports using various java files and this is running 24X7 in the JVM now what happens if any developer changes any java file used by the tool that change is not reflected because newer class files are not loaded .

Please Do let me know if you have any idea

Thanks in advance

Regards

JOLLY
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic