运行时后的Java load jar无法正常工作
当我用post中的代码加载swt jar时,我得到以下异常:运行时后的Java load jar无法正常工作,java,swt,classloader,Java,Swt,Classloader,当我用post中的代码加载swt jar时,我得到以下异常: Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/SWT at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.initialize_(SWTNati
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/SWT
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.initialize_(SWTNativeInterface.java:213)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.initialize(NativeInterface.java:71)
at chrriis.dj.nativeswing.swtimpl.core.SWTNativeInterface.open_(SWTNativeInterface.java:337)
at chrriis.dj.nativeswing.swtimpl.NativeInterface.open(NativeInterface.java:100)
at derby.DerbyProgram.<init>(DerbyProgram.java:161)
at derby.DerbyProgram.getInstance(DerbyProgram.java:130)
at derby.DerbyProgram.main(DerbyProgram.java:121)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.SWT
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
下面是正在访问的SWT.class:
其他的图书馆是和。
有什么想法吗?您的代码都是什么?添加了有关库和代码的更多详细信息。我的其余代码与这个问题无关。的公认答案描述了一种加载SWT Jarst的方法。这似乎与投票率最高的方法没有太大区别。它仍然说找不到该类。
public static void main(String[] args){
try {
loadSWTJar();
}
catch (MalformedURLException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
NativeInterface.open();//This throws the exception
}
private void loadSWTJar() throws MalformedURLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
URLClassLoader classLoader = (URLClassLoader) getClass().getClassLoader();
Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addUrlMethod.setAccessible(true);
String swtFileNameOsPart = osName.contains("win") ? "win" : osName.contains("mac") ? "maco" : osName.contains("linux") || osName.contains("nix") ? "linux" : ""; // throw new RuntimeException("Unknown OS name: "+osName)
String swtFileNameArchPart = osArch.contains("64") ? "64" : "32";
String swtFileName = "swt-" + swtFileNameOsPart + "-" + swtFileNameArchPart + ".jar";
URL swtFileUrl = new URL("file:" + System.getProperty("user.dir") + "/lib/"+swtFileName);
addUrlMethod.invoke(classLoader, swtFileUrl);
System.out.println("Loaded SWT jar: " + swtFileUrl);//This outputs the correct path and file. That file contains the correct stuff in it.
}