Reputation: 641
Hi can anyone guide me in this problem ?
I am reading a .class file using Java reflection and that class contain some other class Reference (External References).But when i try to access/read its information using Java reflection then it will gave
Cannot Find Symbol compile time error?
Is there any way of getting the Custom data Type(means Class A got ref of Class B) of a class using Java reflection in case of reading .class file?
When i try to compile a .java file having external reference then it gave "Symbol not found error" & when i compile it using some IDE i.e eclipse And then try to open its .class file to read it then it gave these errors
Exception in thread "main" java.lang.NoClassDefFoundError: LTeacherClass; at java.lang.Class.getDeclaredFields0(Native Method) at java.lang.Class.privateGetDeclaredFields(Unknown Source) at java.lang.Class.getDeclaredFields(Unknown Source) at ClassExtractManager.classInfoExtracter(ClassExtractManager.java:158) at MainClass.main(MainClass.java:36) Caused by: java.lang.ClassNotFoundException: TeacherClass at java.lang.ClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 5 more
If there still problem then i will post the code.
Upvotes: 1
Views: 3237
Reputation: 4454
Your MyClassLoader
must be aware of other referenced classes. Where are they stored? Make your loader read all these classes before you use java reflection:
public class ABC {
public XYZ getXyz() {
...
}
}
public class XYZ {
...
}
In example above your classloader must be aware of XYZ.class
before you could use reflection on ABC.class
Upvotes: 1
Reputation: 54035
Reflection is a run-time API and I don't think it could give you a compile-time error. Most likely there is an issue with your code such as an undeclared/unimported class or variable.
I see two possibilities.
If you're writing your own classloader or whatever other solution which indeed needs dynamic class loading at run time, you cannot mix it up with references to concrete classes or interfaces unknown (unimported) at compile time. You have to rely on reflection from the beginning to the end, or cast to known types (or, more likely, interfaces, but these have to be known to compiler at compile-time).
Another possibility is that you're misusing reflection or classloading completely. I suspect you may have code that reads a .class file, then try to use that class by name later, assuming it works as if everything was interpreted. That would mean you're messing up compilation and linking with execution. Roughly speaking compiler only translates symbols such as TeacherClass
to relevant code (and so it complains that it doesn't know what TeacherClass
is). Reflection is used after compilation when you actually execute the code. That's why it doesn't affect the compiler.
Perhaps you don't need any of this at all? Maybe you just need to learn the basics of classpath, compilation and linking in Java?
It's all just guesswork based on the scarce information provided.
Upvotes: 2
Reputation: 7414
Try to use ClassLoader.loadClass(String)
to get a class instance from a name. That's the preferred way unless you are implementing your own ClassLoader...
Upvotes: 1