开发者

Is it possible to get the Extension class loader object?

I'm learning about ClassLoading concepts in java. I know that can get the Syste开发者_运维百科m 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜