tintin
tintin

Reputation: 5867

Is it possible to get the Extension class loader object?

I'm learning about ClassLoading concepts in java. I know that can get the System classLoader with ClassLoader.getSystemClassLoader() method using java.lang.ClassLoader class. Is it possible to get Extension class loader also? I'm aware that JVM loads the BootStrapClassLoader and we cannot instantiate it. But what about Extension class loader?

Thanks in Advance.

Upvotes: 1

Views: 1052

Answers (1)

alexsmail
alexsmail

Reputation: 5813

You can try this one sun.net.spi.nameservice.dns.DNSNameService.getClassLoader(). DNSNameService is one the class that exist as the Extension class loader.

Another option is to write something like this:

         ClassLoader cl =new Object(){}.getClass().getEnclosingClass().getClassLoader();
         ClassLoader prev = null;
         while(cl!=null){
             prev=cl;
             cl=cl.getParent();
         }
         System.out.println(prev);

prev will contain reference to extension class loader.

Note: You can write besides new Object(){}.getClass().getEnclosingClass().getClassLoader() idiom Thread.currentThread().getContextClassLoader() or even simpler YourClassName.class.getClassLoader() While these idioms are not identical any of them will do the job.

See http://www.javacodegeeks.com/2011/03/understanding-extending-java.html for some more details.

Upvotes: 1

Related Questions