java反射中的类加载异常

java反射中的类加载异常,java,reflection,Java,Reflection,我使用文件选择器选择了一个jar文件,然后使用java反射加载jar文件中的所有类。有些类依赖于另一个jar文件 Exception in thread "main" java.lang.NoClassDefFoundError: com/thoughtworks/xstream/io/HierarchicalStreamDriver at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.

我使用文件选择器选择了一个jar文件,然后使用java反射加载jar文件中的所有类。有些类依赖于另一个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)
但当我尝试获取类的方法时,会抛出以下异常,因为该类有一个import语句
import com.thoughtworks.xstream.xstream和XStream类在另一个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)
我想知道是否有任何方法可以防止使用java反射加载依赖类或jar文件。下面是我用来加载类的代码

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;
    }
  }
}

好的,如果你的应用程序依赖于这个类,你肯定需要在类路径上有一个包含它的jar(或者提供一个包含包+类的替代路径)。

作为类加载过程的一部分,你想要加载的类所依赖的任何类都会被加载


我认为你对此无能为力。

不确定这是否可行。您还必须加载依赖项库。