Netbeans Platform ClassLoader Trick

October 26th, 2007 | by Tonny Kohar |

As you probably know in Netbeans Platform each module has its own ClassLoader. So if you want to load resources (eg: properties file, xml file, image file, etc) from another module basically you are out of luck.

Just out of my head, there are at least 2 solutions available:

  1. Create Utility or Factory class in other module to return resources
  2. Using other module ClassLoader to resolve the resources

However, if you are not careful the first method could lead to circular dependency problem. So I will focus on the second methods to get the resources from other modules using ClassLoader trick

Note: this is tested on Netbeans 6 beta1

Consider this scenario, you have API/SPI module, which you let other modules to implement that API eg:

– API Module (MaskProvider API)
– Impl Module 1 (MaskProvider Implementation 1)
– Impl Module 2 (MaskProvider Implementation 2)
– Impl Module xxx (MaskProvider Implementation xxx)

MaskProvider API is providing a list of Mask Image, where additional Mask Image could be added by installing the Impl Module xxx. You do not know, who will implement the API and how many implementation is available during design time. The API Module will be loaded on demand, and there is no way for the Impl Module xxx to register itself to API Module using ModuleInstall.restored() eg: Impl Module xxx tell its existence in the layer.xml that will be parsed by API Module when needed.

Now the problem is to allow code in API Module to get resources from registered Impl Module xxx. This is done by passing the Impl Module xxx object into the API Module, so the API Module could load the resources using the passed object ClassLoader.

This passing object could be done by using

  • Using Lookup
    // this code is placed inside API Module, where SomeObject.class is
    // located at Impl Module xxx
    SomeObject obj = Lookup.getDefault().lookup(SomeObject.class);
    SomeResources resources =  obj.getClass().getReourcesAsStream(...);
    // or
    ClassLoader cl = obj.getClass().getClassLoader();
  • Explicitly pass the object
    // This code is placed inside API Module, and the MaskProvider (owner) is
    // located at Impl Module xxx
    public MaskItem(MaskProvider owner,....) {
    	SomeObject resources =  owner.getClass().getReourcesAsStream(...);
    	// or
    	ClassLoader cl = owner.getClass().getClassLoader();
  1. 4 Responses to “Netbeans Platform ClassLoader Trick”

  2. By David Strupl on Oct 26, 2007 | Reply

    You can use Lookup.getDefault().lookup(ClassLoader.class);
    This will get you classloader capable of loading resources from any enabled module. Hope this helps, David

  3. By James on Oct 31, 2007 | Reply

    Hi Tony,

    Any chance you could contribute this to the NetBeans Community Docs wiki? It would be a great tips & tricks entry.



  4. By Tonny Kohar on Nov 1, 2007 | Reply


    Done, I just added this blog entry to the Netbeans Community Docs wiki

  5. By Anonymous Poster on Feb 10, 2008 | Reply

    I do not know if Lookup.getDefault().lookup(ClassLoader.class) solves the problem above, but it fixed a resource issue I was having and I want to say thanks for the inspiration David Strupl

You must be logged in to post a comment.