在java中读取.class文件
嗨,有人可以指导我解决这个问题吗?
我正在使用Java反射读取.class文件,并且该类包含一些其他类引用(外部引用)。但是当我尝试使用Java反射访问/读取其信息时,它会给出
找不到符号编译时错误?
在读取 .class 文件时,有没有办法使用 Java 反射来获取类的自定义数据类型(意味着 A 类获得了 B 类的引用)?
- 我正在使用 ClassLoader 读取 .class 文件
- ,然后我正在读取该文件,然后将其传输到 字节数组 并使用 defineCalss() 方法将获得类对象。
- 在我获得信息(即类的所有字段)后,我使用 getDeclaredFields() 方法。
当我尝试编译具有外部引用的 .java 文件时,它给出了“符号未找到错误” &当我使用一些IDE(即eclipse)编译它然后尝试打开它的.class文件来读取它然后它
在线程“main”java.lang.NoClassDefFoundError中给出了这些错误异常:LTeacherClass; 在 java.lang.Class.getDeclaredFields0(本机方法) 在 java.lang.Class.privateGetDeclaredFields(来源未知) 在 java.lang.Class.getDeclaredFields(来源未知) 在 ClassExtractManager.classInfoExtracter(ClassExtractManager.java:158) 在 MainClass.main(MainClass.java:36) 引起原因:java.lang.ClassNotFoundException:TeacherClass 在 java.lang.ClassLoader.findClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) ... 5 更多
如果仍然存在问题,那么我将发布代码。
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?
- I am reading a .class file using ClassLoader
- then i am reading the file and then transfer it into byte array and using defineCalss() method i will get the Class Object.
- After i will get information i.e. all fields of the class i use getDeclaredFields() method.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
反射是一个运行时 API,我认为它不会给您带来编译时错误。您的代码很可能存在问题,例如未声明/未导入的类或变量。
我看到两种可能性。
如果您正在编写自己的类加载器或任何其他确实需要在运行时动态类加载的解决方案,则不能将其与编译时未知(未导入)的具体类或接口的引用混合在一起。您必须从头到尾依赖反射,或者转换为已知类型(或者更可能的是接口,但这些必须在编译时为编译器所知)。
另一种可能性是您完全滥用了反射或类加载。我怀疑您可能有读取 .class 文件的代码,然后尝试稍后按名称使用该类,假设它的工作方式就像所有内容都已解释一样。这意味着你搞乱了编译和链接执行。粗略地说,编译器仅将诸如
TeacherClass
之类的符号转换为相关代码(因此它抱怨它不知道TeacherClass
是什么)。反射是在编译后实际执行代码时使用的。这就是为什么它不会影响编译器。也许您根本不需要这些?也许您只需要学习 Java 中的类路径、编译和链接的基础知识?
这一切都只是基于所提供的稀缺信息的猜测。
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 whatTeacherClass
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.
尝试使用 ClassLoader.loadClass(String) 从名称获取类实例。这是首选方式,除非您要实现自己的类加载器...
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...您的
MyClassLoader
必须了解其他引用的类。它们存储在哪里?让您的加载器在使用 java 反射之前读取所有这些类:在上面的示例中,您的类加载器必须先了解
XYZ.class
,然后才能使用对 ABC.class 的反思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:In example above your classloader must be aware of
XYZ.class
before you could use reflection onABC.class