2011-07-13 9 views
0

ファイルセレクタを使用してjarファイルを選択し、Javaリフレクションを使用してjarファイルのすべてのクラスをロードしました。クラスによっては、別のjarファイルに依存するものがあります。クラスの読み込みJavaのリフレクションの例外

しかし、クラスのメソッドを取得しようとすると、このクラスにインポートステートメント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; 
    } 
    } 
} 
+0

これが可能かどうかわかりません。依存関係ライブラリもロードする必要があります。 –

答えて

1

さて、あなたのアプリケーションは、そのクラスに依存する場合、ほとんど間違いなくあなたは、それを含むjarファイルを持っている必要があります(またはパッケージ+クラスを含む代替パスを提供)クラスパス上に。

0

クラスローディングプロセスの一部として、ロードしたいクラスが依存するクラスもロードされます。

あなたができることは何もないと思います。

関連する問題