简体   繁体   English

Java反射导入

[英]Java Reflection Imports

Just a quick question regarding Java's reflection mechanism. 关于Java反射机制的一个简单问题。 My program should load the .class items from a .jar file. 我的程序应从.jar文件加载.class项目。

I get a java.lang.NoClassDefFoundError for classes within the jar file, that are importing outer packages (to which I don't have access). 对于jar文件中的类,我收到一个java.lang.NoClassDefFoundError ,这些类正在导入外部程序包(我没有访问权限)。 Is there any chance of passing the exception without having to import the required packages. 是否有可能通过传递异常而不必导入所需的程序包。

Thanks. 谢谢。

I don't believe so - in order for reflection to load, you have to load the class using a class loader and then can, for example, instantiate objects and execute methods, so you will need the dependencies. 我不这么认为-为了加载反射,您必须使用类加载器加载类,然后才能实例化对象和执行方法,因此您将需要依赖项。

If you don't want to instantiate or execute, then a bytecode library would allow you to inspect the contents of the class file. 如果您不想实例化或执行,那么字节码库将允许您检查类文件的内容。

Others are correct, you cannot import code from dependencies of dependencies, directly. 其他的是正确的,您不能直接从依赖项的依赖关系中导入代码。

That being said, a great way to prevent this problem is to use build technologies like Maven and Gradle. 话虽这么说,防止此问题的好方法是使用Maven和Gradle等构建技术。 When you import dependencies with either of those techs, you automatically import dependencies of dependencies. 使用任何一种技术导入依赖项时,都会自动导入依赖项的依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM