简体   繁体   English

Class 在 java 反射中加载异常

[英]Class loading Exception in java reflection

I have selected a jar file using file selector, then loaded all the classes in the jar file using java reflection.我使用文件选择器选择了 jar 文件,然后使用 java 反射加载了 jar 文件中的所有类。 Some classes has dependency on another jar file.某些类依赖于另一个 jar 文件。

But when I try to get method of class then following exception is thrown because this class has a import statement import com.thoughtworks.xstream.XStream;但是,当我尝试获取 class 的方法时,会引发以下异常,因为此 class 具有导入语句import com.thoughtworks.xstream.XStream; and XStream class is defined in another jar file.和 XStream class 在另一个 jar 文件中定义。

Exception in thread "main" java.lang.NoClassDefFoundError: com/thoughtworks/xstream/io/HierarchicalStreamDriver
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2365)
    at java.lang.Class.privateGetPublicMethods(Class.java:2488)
    at java.lang.Class.getMethods(Class.java:1406)
    at com.axway.xtp.testgenerator.templatewizard.MethodSelectionWizardUI.updateListofMethods(MethodSelectionWizardUI.java:744)
    at com.axway.xtp.testgenerator.templatewizard.MethodSelectionWizardUI$7.widgetSelected(MethodSelectionWizardUI.java:474)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:90)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:928)

I wanted to know that is there any way to prevent the dependency class or jar file to be loaded using java reflection.我想知道有什么方法可以防止使用 java 反射加载依赖 class 或 jar 文件。 Following are the piece of code I am using to load classes.以下是我用来加载类的一段代码。

URLClassLoader ucl = new URLClassLoader(new URL[] { new URL("file:" + codeRepository) });
JarFile jarFile = new JarFile(new File(codeRepository));
Enumeration enm = jarFile.entries();
while (enm.hasMoreElements()) {
  JarEntry entry = ((JarEntry) enm.nextElement());

  if (entry.getName().endsWith(".class")) {
    String fullClassNameWithPath = entry.getName();
    String fullyClassifiedClassName = fullClassNameWithPath
        .replace('/', '.');

    try {
      Class c = ucl.loadClass(fullyClassifiedClassName.substring(
          0, fullyClassifiedClassName.indexOf(".class")));
      String className = c.getPackage().getName() + "."
          + c.getSimpleName();
      listClasses.add(className);
    } catch (Exception e) {
      continue;
    } catch (Throwable t) {
      continue;
    }
  }
}

Well, if your application depends on that class, you most definitely need to have the jar containing it (or provide an alternative path containing the package+class) on the classpath.好吧,如果您的应用程序依赖于 class,那么您肯定需要在类路径上包含它(或提供包含包+类的替代路径)的 jar。

As part of the classloading process, any classes which the class you want to load depends upon will also be loaded.作为类加载过程的一部分,您要加载的 class 所依赖的任何类也将被加载。

I don't think there's anything you can do about that.我认为你对此无能为力。

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

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