access fields inside a class that reference to an external object not in cp
Hello Friends,
I am doing a jar browser application. i have a problem when i try to access a
Field inside a class that reference to an external object not in the classpath
for example class A with a static field that reference a org.apache.commons.logging.Log
Class A{
privatestatic Logger logger = ... ;
.
.
.
}
if a do
Class classReader{
public classReader{
Class cls = Class.forName("A");// load class ok
Field fields[] = cls.getDeclaredFields();// throws an exception
}
}
i get an Exception
Exception in thread "Thread-2" java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2232)
my question is: Is there any way to read classes with fields that reference to external objects not in the classpath? i don't need to load the class i just want to know what type the field is.
i know that netbeans file panel can do it.
i hope you understand my problem.
Thanks
Message was edited by:
Marcov8
[1528 byte] By [
Marcov8a] at [2007-10-3 5:12:43]

You have loaded that class A but doesn't mean that you can access its field.
You must be carefull, when you use java reflection to read fields or methods than filelds must
be annotated with public modifier.Try your code again
but try to change
private static Logger logger;
in public static Logger logger.
Yes is true, in order to be able to read the fields i need the object that i don't have.
but how does Netbeans, 'Files windows', is able to read the fields even the object is not accessible?
by not accessible i mean the file .class is not present in the jar file.
by read the field i mean to read the qualifier and the name of the field not the value holded by it.
Marco
Thanks.
Message was edited by:
Marcov8
NetBeans and Eclipse and other IDEs don't use reflection. Lots of reasons for this, but the issue you're having is a big one. Reflection just isn't built for the kind of work you're apparently trying to do. To get the fields of the class you need, java needs to know all their types. If one of the types is a class that is not available to the class loader, there's nothing java can do, so it throws an exception at you. You need to find some third-party software that can parse a .class file into a structure your program can read. I don't know of any off hand, but I'm sure they exist.