• 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Classloader quizzer

Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test() {

public static void main(String[] args) {

CustomClassLoader ccl = new CustomClassLoader();
Foo foo = new Foo();

In the above.. why is the class Foo not loaded by the custom class loader?
What do I need to do to make Foo load using the ccl classloader.

I know I can do this..

ccl.loadClass("Foo").newInstance() instead of new Foo(). But Foo also instantiates class SubFoo in return using the new operator and that point I would have to replace the new SubFoo() call with ccl.loadClass("SubFoo").newInstance().

I don't want to replace all "new" calls with ccl.loadClass(...) .
Especially since Foo/SubFoo could be in a third party jar.
[ October 13, 2006: Message edited by: Chase Bonham ]
author and iconoclast
Posts: 24204
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The context classloader is actually not as magical as one would hope. It's there, and you're supposed to use it if you're writing tools and containers and the like -- but it's not as though the JVM actually uses it for anything; only libraries do.

There's a flaw in your chain of inferences here, and that's good news for you: the classes loaded as a result of loading Foo will be loaded by the ClassLoader that loaded Foo, automatically. So you only have to load one class to create a new "world".
Grow a forest with seedballs and this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic