gpt4 book ai didi

java - 奇怪的类路径,.NoClassDefFoundError 错误

转载 作者:行者123 更新时间:2023-12-02 08:37:36 27 4
gpt4 key购买 nike

已编辑:找到解决方案。这很奇怪,不是最好的解决方案,但我只是继续将我的 JAVA 代码 (com.*) 中的类放入 swt.jar 中,以便 swt.jar 和我的类在完全相同的时刻由相同的时间加载类加载器。不是最好的解决方案,而是临时解决方案

编辑:我在此处添加了大部分代码: http://groups.google.com/group/clojure/browse_thread/thread/1d7dd4913b2f9aa7 http://groups.google.com/group/clojure/web/NoClassDefIssue.zip http://groups.google.com/group/clojure/web/MostCodeIssue.java

这是奇怪的 no classdef not found 错误。下面的场景与我的 90% 的代码工作略有不同。请忽略我正在使用 Clojure,我也使用常规 java 代码完成了此操作,但仍然遇到相同的错误。

理想情况下,我希望在自定义类加载器上获得一些帮助。我最初的想法是我可以创建类加载器,将 jar 的文件路径/url 添加到类加载器中,然后我可以启动我的代码。但显然发生了一些奇怪的事情。

  1. 我有一个 jar 文件,可以动态加载 4-5 个辅助 jar 文件。它构建在一个简单的 URLClassLoader 之上。我想这样做是因为我可以让用户单击一个 jar 文件并启动该应用程序。因此,有一项是用户必须担心的。

  2. 通过单击此 jar 文件,应用程序从文件系统加载 jar 文件。例如。应用程序加载 swt.jar(Eclipse 的小部件工具包)和一些其他 jar。

3(但不用担心),我的应用程序是基于 Clojure(动态编程语言)构建的,第一个启动的应用程序是这个 Clojure 脚本。我的大部分应用程序都在 clojure 脚本内。

4。 1-3 以内的所有内容都可以正常工作。应用程序加载、clojure 脚本运行、SWT GUI 应用程序运行等。

  • 这是不起作用的部分。

  • 我有一个现有的 java 库,将其命名为 my-swt-gui.jar。那也是一个swt应用程序。它基本上是另一个已经构建的 GUI 应用程序。我正在尝试从当前的 clojure/swt 应用程序加载窗口。由于某种原因,面向 JAVA 的库无法识别 SWT,并且我收到 noclassdef 错误。

  • 奇怪的是。我会找出我认为可能存在奇怪之处的地方。

    实体炎:A. 面向java的类加载器。在 java 类加载器中,我启动 clojure 应用程序。 SWT 和其他 jar 文件通过该类加载器加载。B. 由节实体调用的面向 Clojure 的代码clojure 代码是一个基于 swt 的 GUI 应用程序。 B 工作正常。C. 包含另一个 GUI 窗口的 Java 代码。由于某种原因,该库无法加载,并且我收到错误 noclassdef。

    注意:我知道 SWT 类实际上位于类路径中,否则我会得到 NoClassFound 异常。我没有得到这个异常(exception)。还有其他事情正在发生。

    注意:这是否也是 SWT 包含 win32 dll 的问题?也许第一次访问 win32 dll jar 可以工作,但是使用 java 代码就不行了?但这很奇怪。为什么 SWT 可以工作然后又不行?

    我无法向您展示所有代码,我希望可以向您展示相关部分。

    这是主要的类加载器代码:

     public static final String [] JAVA_LIBRARIES = {
    "lib\\log4j-1.2.15.jar",
    "lib\\octane_commons.jar",
    "lib\\clojure.jar",
    "lib\\swt\\win32\\swt.jar",
    "lib\\jfreechart\\jcommon-1.0.15.jar",
    "lib\\jfreechart\\jfreechart-1.0.12.jar",
    "lib\\jfreechart\\jfreechart-1.0.12-swt.jar",
    "lib\\pdf\\minium.jar",
    "lib\\pdf\\tagsoup-1.2.jar",
    "lib\\pdf\\core-renderer.jar",
    "lib",
    "conf",
    "src"
    };

    My classloader code is based on jetty's classloader and it works OK
    but I keep get ting classnotdef errors. Strange ones. I can see a
    clear distinction between classnotfound errors. If the file path to
    the jar is invalid then I get classnotfound, easy to detect and fix.

    Here is essentially the classloader code for future reference.

    Classpath classpath = new Classpath();

    boolean res = classpath.addComponent(libFilePath);

    /// Classloader

    private class Loader extends URLClassLoader {
    String name;
    Loader(URL[] urls, ClassLoader parent) {
    super(urls, parent);
    name = "StartLoader" + Arrays.asList(urls);
    }

    public String toString() {
    return name;
    }
    }

    //// Then set the classloader
    //// where the URLs are the JAR libraries:
    URL [] urls = new URL[NUMBER_OF_JARS];
    for (x in urls) {
    urls[i] = new URL("THE JAR PATH");
    }

    ClassLoader parent = Thread.currentThread().getContextClassLoader();
    if (parent == null) {
    parent = Classpath.class.getClassLoader();
    }
    if (parent == null) {

    parent = ClassLoader.getSystemClassLoader();
    }
    return new Loader(urls, parent);

    /////////////
    <小时/>

    这就是我尝试加载 java swt 窗口的方法。我尝试打印尽可能多的类加载器信息并更改为不同的类加载器。

    公共(public)静态最终无效createPDFWindowShell(IStartService服务,最终对象外壳,最终对象globalClassLoader)抛出异常{

    if ((service != null) && (shell != null)) {
    synchronized(service) {
    service.runService();
    Thread.sleep(80);
    }
    } else {
    System.err.println("Invalid arguments : service => " + service + " shell =>" + shell);
    return;
    } // End of the if //

    final ClassLoader cl = globalClassLoader == null ? service.getClass().getClassLoader() : (ClassLoader) globalClassLoader;
    System.out.println("Service Class Loader.1: " + service.getClass().getClassLoader());
    System.out.println("Service Class Loader.2 : " + cl);
    System.out.println("-----------------");
    System.out.println("[From DynaClass.1] : " + cl.loadClass("com.octane.start.services.IStartService"));
    System.out.println("[From DynaClass.2] : " + cl.loadClass("org.eclipse.swt.SWT"));
    System.out.println("[From DynaClass.3] : " + cl.loadClass("org.eclipse.swt.events.SelectionListener"));
    System.out.println("-----------------");

    final Class listenerTargetClass = cl.loadClass("org.eclipse.swt.events.SelectionListener");
    final ClassLoader newCl = globalClassLoader == null ? service.getClass().getClassLoader() : (ClassLoader) globalClassLoader;

    // Now launch the shell //
    //final Class winClass = Class.forName("com.ca.util.gui.SimplePFSXHtmlPDFWin");
    final Class winClass = cl.loadClass("com.ca.util.gui.SimplePFSXHtmlPDFWin");
    final Method methodCreate = winClass.getMethod("createPDFWindowShell", new Class [] { Object.class });
    methodCreate.invoke(null, shell);

    }

    <小时/>

    这是错误:

     [java] [INFO : Classpath Loader Check] : valid files=17 / total=17
    [java] [Classpath Loader] - thread class loader parent == false
    [java] [INFO : Classpath Loader Check] : classpath=StartLoader[file:/C%3a/Program%20Files/Java/jdk1.5.0_11/lib/tools.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/log4j-1.2.15.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/octane_commons.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/clojure.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/swt/win32/swt.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jcommon-1.0.15.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12-swt.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/gnujaxp.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/swtgraphics2d.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12-experimental.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/xercesImpl.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/minium.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/tagsoup-1.2.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/core-renderer.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/, file:/C%3a/usr/local/pfs/projects/octane/conf/, file:/C%3a/usr/local/pfs/projects/octane/src/]
    [java] Active threads : 3
    [java] class clojure.lang.Script
    [java] class org.eclipse.swt.SWT
    [java] interface org.eclipse.swt.events.SelectionListener
    [java] class org.apache.log4j.Logger
    [java] class org.xhtmlrenderer.util.XRLog
    [java] [INFO] Thread ID : Thread[Thread-3,5,main]
    [java] Service Class Loader.1: sun.misc.Launcher$AppClassLoader@92e78c
    [java] Service Class Loader.2 : StartLoader[file:/C%3a/Program%20Files/Java/jdk1.5.0_11/lib/tools.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/log4j-1.2.15.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/octane_commons.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/clojure.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/swt/win32/swt.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jcommon-1.0.15.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12-swt.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/gnujaxp.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/swtgraphics2d.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/jfreechart/jfreechart-1.0.12-experimental.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/xercesImpl.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/minium.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/tagsoup-1.2.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/pdf/core-renderer.jar, file:/C%3a/usr/local/pfs/projects/octane/lib/, file:/C%3a/usr/local/pfs/projects/octane/conf/, file:/C%3a/usr/local/pfs/projects/octane/src/]
    [java] -----------------
    [java] [From DynaClass.1] : interface com.octane.start.services.IStartService
    [java] [From DynaClass.2] : class org.eclipse.swt.SWT
    [java] [From DynaClass.3] : interface org.eclipse.swt.events.SelectionListener
    [java] -----------------
    [java] java.lang.reflect.InvocationTargetException
    [java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java] at java.lang.reflect.Method.invoke(Method.java:585)
    [java] at com.octane.start.OctaneClojureScript.invokeVarInvoke(OctaneClojureScript.java:89)
    [java] at com.octane.start.OctaneClojureScript.invokeContract(OctaneClojureScript.java:118)
    [java] at com.octane.start.OctaneClojureScript.main(OctaneClojureScript.java:129)
    [java] at com.octane.start.services.ClassPathLoaderService.runService(ClassPathLoaderService.java:225)
    [java] at com.octane.start.OctaneLauncherMain.runClasspathService(OctaneLauncherMain.java:73)
    [java] at com.octane.start.OctaneLauncherMain.main(OctaneLauncherMain.java:97)
    [java] Caused by: java.lang.NoClassDefFoundError: org/eclipse/swt/events/SelectionListener (octane_main_window.clj:0)
    [java] at clojure.lang.Compiler.eval(Compiler.java:4543)
    [java] at clojure.lang.Compiler.load(Compiler.java:4857)
    [java] at clojure.lang.RT.loadResourceScript(RT.java:326)
    [java] at clojure.lang.RT.loadResourceScript(RT.java:317)
    [java] at clojure.lang.RT.load(RT.java:395)
    [java] at clojure.lang.RT.load(RT.java:367)
    [java] at clojure.core$load__5058$fn__5061.invoke(core.clj:3734)
    [java] at clojure.core$load__5058.doInvoke(core.clj:3733)
    [java] at clojure.lang.RestFn.invoke(RestFn.java:413)
    [java] at clojure.core$load_one__5010.invoke(core.clj:3578)
    [java] at clojure.core$load_lib__5031.doInvoke(core.clj:3615)
    [java] at clojure.lang.RestFn.applyTo(RestFn.java:147)
    [java] at clojure.core$apply__3243.doInvoke(core.clj:390)
    [java] at clojure.lang.RestFn.invoke(RestFn.java:443)
    [java] at clojure.core$load_libs__5043.doInvoke(core.clj:3641)
    [java] at clojure.lang.RestFn.applyTo(RestFn.java:142)
    [java] at clojure.core$apply__3243.doInvoke(core.clj:390)
    [java] at clojure.lang.RestFn.invoke(RestFn.java:443)
    [java] at clojure.core$require__5049.doInvoke(core.clj:3701)
    [java] at clojure.lang.RestFn.invoke(RestFn.java:413)
    [java] at clojure.lang.Var.invoke(Var.java:346)
    [java] ... 10 more
    [java] Caused by: java.lang.NoClassDefFoundError: org/eclipse/swt/events/SelectionListener
    [java] at java.lang.Class.getDeclaredMethods0(Native Method)
    [java] at java.lang.Class.privateGetDeclaredMethods(Class.java:2395)
    [java] at java.lang.Class.getMethod0(Class.java:2642)
    [java] at java.lang.Class.getMethod(Class.java:1579)
    [java] at com.octane.start.PDFDynamicStartWin.createPDFWindowShell(PDFDynamicStartWin.java:39)
    [java] at octane.toolkit.octane_core_widgets$pdf_handler__2659.invoke(octane_core_widgets.clj:81)
    [java] at octane.toolkit.octane_core_widgets$fn__2662$fn__2664.invoke(octane_core_widgets.clj:87)
    [java] at clojure.proxy.org.eclipse.swt.events.SelectionAdapter.widgetSelected(Unknown Source)
    [java] at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
    [java] at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    [java] at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    [java] at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    [java] at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    [java] at octane.toolkit.octane_main_window$create_gui_window__2990.invoke(octane_main_window.clj:234)
    [java] at octane.toolkit.octane_main_window$main_1__2994.invoke(octane_main_window.clj:246)
    [java] Processing Time : 6235
    [java] at octane.toolkit.octane_main_window$_main__2998.doInvoke(octane_main_window.clj:250)
    [java] at clojure.lang.RestFn.invoke(RestFn.java:402)
    [java] at octane.toolkit.octane_main_window$eval__3001.invoke(octane_main_window.clj:259)
    [java] at clojure.lang.Compiler.eval(Compiler.java:4532)
    [java] ... 30 more

    -

    最佳答案

    我在一个更简单的场景中看到了类似的问题,其中子类加载器加载的类尝试获取父类加载器加载的类以动态加载子类加载器的同级之一 - 它不起作用,因为子->父关系只是单向的。

    如果没有所有代码,我无法确定,但我怀疑正在发生类似以下的事情:

    1. “全局”类加载器委托(delegate)类加载器 A 加载 SWT 类
    2. “全局”类加载器委托(delegate)类加载器 B 加载 Clojure 代码
    3. Clojure 代码尝试通过它所知道的唯一类加载器(即类加载器 B)动态加载 SWT 类
    4. 类加载器 B 不知道在哪里找到它们,然后崩溃了

    这有帮助吗?

    关于java - 奇怪的类路径,.NoClassDefFoundError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1060659/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com