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]
# 1
i guess my question makes no sense. could anyone confirm that. so i stop tryingThanks
Marcov8a at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 2

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.

EqAfricaa at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 3
Ok thanks.
Marcov8a at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 4
I'm not totally sure, but this has nothing to do with the fields modifier. IMHO, your initial remark is correct that it cannot access the Logger.class object, which it needs for the Field-Object representing the respective field (as it holds a reference to the field's type.
stefan.schulza at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 5

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

Marcov8a at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 6

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.

Updownquarka at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 7
thanks.your answer makes sense.i need a .class parser.bye
Marcov8a at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...
# 8

You understand of course that to parse the class file you must actually have the class file?

And once you have the class file you could load it. The parser precludes the requirement for loading it though. However what I you going to do if one of the fields in there is actually a reference to another class?

jschella at 2007-7-14 23:19:08 > top of Java-index,Core,Core APIs...