Possible Bug in the way NetBeans loads resources from within MODULES
We are using NetBeans IDE 3.4 RC1 and we are having the following
problems loading resources. Are these bugs in NetBeans or what?
From what I can tell, it looks as if NetBeans Modules cannot load
resources from the root of the CLASSPATH ("/") and it also looks like the
ClassLoader.getResource () and ClassLoader.getResourceAsStream () is buggy.
Thanks in advance,
Jason Sapp
--
- Create a small module where the following class (LoadResourceTester)
executes and the following resources are in the MODULE's JAR file:
/abc.txt
/com/abccompany/abc.txt
-
package com.abccompany.test;
import java.io.*;
public class LoadResourceTester
{
public LoadResourceTester ()
{
// **** THIS IS A PROBLEM **** //
// The following line returns NULL, but it should return an InputStream
on the resource.
getClass ().getResourceAsStream ("/abc.txt");
// **** THIS IS NOT A PROBLEM **** //
// The following line finds the resource and returns an InputStream,
which makes sense.
getClass ().getResourceAsStream ("/com/abccompany/abc.txt");
// **** THIS IS A PROBLEM **** //
// The following line returns NULL, but it should return an InputStream
on the resource.
getClass ().getClassLoader ().getResourceAsStream ("/abc.txt");
// **** THIS IS A PROBLEM **** //
// The following line returns NULL, but it should return an InputStream
on the resource.
getClass ().getClassLoader ().getResourceAsStream
("/com/abccompany/abc.txt");
// **** THIS IS A PROBLEM **** //
// The following line finds the resource, but it should return NULL (at
least, in my opinion).
getClass ().getClassLoader ().getResourceAsStream
("com/abccompany/abc.txt");
}
}
-
[1914 byte] By [
Sapp,Jason] at [2007-11-25 16:51:04]

# 1
Jason Sapp wrote:
>We are using NetBeans IDE 3.4 RC1 and we are having the following
> problems loading resources. Are these bugs in NetBeans or what?
First of all, if you are writing a module, please use
dev@openide.netbeans.org not nbusers@netbeans.org - this list is
intended for more end users of the product.
>From what I can tell, it looks as if NetBeans Modules cannot load
> resources from the root of the CLASSPATH ("/") and it also looks like the
> ClassLoader.getResource () and ClassLoader.getResourceAsStream () is buggy.
>
> - Create a small module where the following class (LoadResourceTester)
> executes and the following resources are in the MODULE's JAR file:
>
> /abc.txt
> /com/abccompany/abc.txt
>
>// The following line returns NULL, but it should return an InputStream
> on the resource.
>getClass ().getResourceAsStream ("/abc.txt");
Yup, NB will refuse to load anything from the default package. This is a
safety measure, as otherwise modules would be clobbering one another's
resources all over. Furthermore, no two modules are permitted to load
classes or resources from the same package (*). You must choose a
package namespace and stick to it.
// The following line finds the resource, but it should return NULL
// (at least, in my opinion).
getClass().getClassLoader().getResourceAsStream("com/abccompany/abc.txt");
is fine, why should this return null? Actually this:
getClass().getClassLoader().getResourceAsStream("/com/abccompany/abc.txt");
is more likely to cause performance or semantic problems on some JDKs
(independently of NB), if I recall correctly, so please use the first form.
-Jesse
(*) Technically: it can be done, but if another module depends on both,
it is undefined from which module it will find resources in that
package, and furthermore it will find all resources for that package
from one or the other parent module, not both. So if that confused you,
summary: don't try it.
--
Jesse Glick <mailto:jesse.glick@sun.com> x22801
NetBeans, Open APIs <http://www.netbeans.org/>