编辑:找到解决方案。这很奇怪,不是最好的解决方案,但我只是继续将我的 JAVA CODE (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 添加到类加载器中,然后我可以启动我的代码。但显然发生了一些奇怪的事情。
我有一个动态加载 4-5 个辅助 jar 文件的 jar 文件。它建立在一个简单的 URLClassLoader 之上。我想这样做是因为我可以让用户单击一个 jar 文件并启动应用程序。所以有一个项目是用户必须担心的。
通过单击此 jar 文件,应用程序从文件系统加载 jar 文件。例如,应用程序加载 swt.jar(Eclipse 的小部件工具包)和其他一些 jars。
3(但不用担心),我的应用程序是基于 Clojure(动态编程语言)构建的,第一个启动的应用程序是这个 Clojure 脚本。我的大部分应用程序都在 clojure 脚本中。
4. 1-3 以内的一切都可以正常工作。应用程序加载和 clojure 脚本运行,SWT GUI 应用程序运行等。
这是不起作用的部分。
我有一个现有的 java 库,称之为 my-swt-gui.jar。那也是一个 swt 应用程序。它基本上已经构建了另一个 gui 应用程序。我正在尝试从我当前的 clojure/swt 应用程序加载窗口。由于某种原因,面向 JAVA 的库无法识别 SWT,并且出现 noclassdef 错误。
这就是奇怪之处。我将确定我认为可能存在异常的地方。
Entitis:A. 面向 java 的类加载器。在 java 类加载器中,我启动了 clojure 应用程序。SWT 和其他 jar 文件使用这个类加载器加载。B. 节实体调用的面向 Clojure 的代码 clojure 代码是一个基于 swt 的 GUI 应用程序。B 工作正常。C. 包含另一个 GUI 窗口的 Java 代码。出于某种原因,这个库不会加载,我得到了错误,noclassdef。
注意:我知道 SWT 类实际上在类路径中,否则我会得到 NoClassFound 异常。我没有得到那个例外。其他事情正在发生。
注意: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 窗口的方式。我试图打印出尽可能多的关于类加载器并更改为不同的类加载器。
公共静态最终无效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
-