[英]How to solve a NoClassDefFoundError caused by ClassNotFoundException?
I'm trying to make an extension for BlueJ (this is a free Java Development Environment
designed for beginners).我正在尝试为BlueJ做一个扩展(这是一个为初学者设计的免费
Java Development Environment
)。 The Extension should help the first year students in our school.扩展应该帮助我们学校的一年级学生。 The extension has to be a JAR file, that I have to place in a folder where
BlueJ
can load it at runtime
.扩展名必须是一个 JAR 文件,我必须将它放在
BlueJ
可以在runtime
加载它的文件夹中。 My already written extension can be loaded from there and it works without any problem.我已经写好的扩展可以从那里加载,它可以正常工作。
Now to the reason, for this question: I want to integrate checkstyle (this is a development tool to help programmers write Java code that adheres to a coding standard.) into my extension.现在来看看这个问题的原因:我想将checkstyle (这是一种帮助程序员编写符合编码标准的 Java 代码的开发工具。)集成到我的扩展中。 So I put the
checkstyle jar
within my project and tried to use this library inside my code.所以我将
checkstyle jar
放在我的项目中,并尝试在我的代码中使用这个库。 If I now generate a jar and copy my extension into the BlueJ
folder and start BlueJ
out of eclipse I get the following error:如果我现在生成一个 jar 并将我的扩展复制到
BlueJ
文件夹并从 Eclipse 中启动BlueJ
,我会收到以下错误:
java.lang.NoClassDefFoundError: com/puppycrawl/tools/checkstyle/api/CheckstyleException at ch.ntb.sir.ntbbluejextension.NtbBlueJExtension.startup(NtbBlueJExtension.java:79) at bluej.extmgr.ExtensionWrapper.safeStartup(ExtensionWrapper.java:533) at bluej.extmgr.ExtensionWrapper.newExtension(ExtensionWrapper.java:209) at bluej.extmgr.ExtensionsManager.loadDirectoryExtensions(ExtensionsManager.java:164) at bluej.extmgr.ExtensionsManager.loadExtensions(ExtensionsManager.java:100) at bluej.extmgr.ExtensionsManager.getInstance(ExtensionsManager.java:61) at bluej.Main.(Main.java:101) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at java.lang.Class.newInstance(Class.java:442) at bluej.Boot.bootBluej(Boot.
java.lang.NoClassDefFoundError: com/puppycrawl/tools/checkstyle/api/CheckstyleException at ch.ntb.sir.ntbbluejextension.NtbBlueJExtension.startup(NtbBlueJExtension.java:79) at bluej.extmgr.ExtensionWrapper.safeStartup(ExtensionWrapper.java:533) ) 在 bluej.extmgr.ExtensionWrapper.newExtension(ExtensionWrapper.java:209) 在 bluej.extmgr.ExtensionsManager.loadDirectoryExtensions(ExtensionsManager.java:164) 在 bluej.extmgr.ExtensionsManager.loadExtensions(ExtensionsManager.java:100) 在 bluej.ext .ExtensionsManager.getInstance(ExtensionsManager.java:61) at bluej.Main.(Main.java:101) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:422) 在 java.lang.Class.newInstance(Class.java:442) 在 bluej。 Boot.bootBluej(Boot. java:348) at bluej.Boot.main(Boot.java:175) Caused by: java.lang.ClassNotFoundException: com.puppycrawl.tools.checkstyle.api.CheckstyleException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 14 more
java:348) at bluej.Boot.main(Boot.java:175) 由:java.lang.ClassNotFoundException: com.puppycrawl.tools.checkstyle.api.CheckstyleException at java.net.URLClassLoader.findClass(URLClassLoader.java: 381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 14 更多
At NtbBlueJExtension.java:79
I make this initialization:在
NtbBlueJExtension.java:79
我进行了这个初始化:
`codeAnalysisHandler = new CodeAnalysisHandler();`
Inside the CodeAnalysisHandler
class I have imports from the checkstyle libraries
:在
CodeAnalysisHandler
类中,我从checkstyle libraries
导入:
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.AuditListener;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
My first though is, that there I should find the cause of my problem?我的第一个问题是,我应该在那里找到问题的原因? But then I have no idea what to search and how to solve it.
但是后来我不知道要搜索什么以及如何解决它。
I've added the jar in my project properties in eclipse.我在 eclipse 的项目属性中添加了 jar。 The Jar is inside the project structure in my
lib
folder. Jar 位于我的
lib
文件夹中的项目结构内。 BlueJ
had no problem to load the bluejext.jar
from the "lib" folder. BlueJ
没有问题加载bluejext.jar
从“Lib”文件夹中。 The bluejext.jar
comes from BlueJ
and is required to write an extension for BlueJ.该
bluejext.jar
来自BlueJ
,并须写BlueJ的一个扩展。 I have this manifest file:我有这个清单文件:
Manifest-Version: 1.0
Main-Class: ch.ntb.sir.ntbbluejextension.NtbBlueJExtension
Class-Path: lib/bluejext.jar lib/checkstyle-all.jar
I tried to google it, but found mostly information about how difficult it is to find the reason.我试图用谷歌搜索它,但发现的主要信息是关于找到原因的难度。
EDIT 1: I unzipped my JAR file to look, if the checkstyle-all.jar
is really inside my extension and there I found a file .classpath
:编辑 1:我解压我的 JAR 文件来查看,如果
checkstyle-all.jar
真的在我的扩展中并且我在那里找到了一个文件.classpath
:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib/bluejext.jar"/>
<classpathentry kind="lib" path="lib/checkstyle-all.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
SOLUTION: Use Eclipse Kepler with the fat jar plugin (Neon was to new for the plugin) and export the project with ´Build Fat Jar´.解决方案:使用 Eclipse Kepler 和fat jar 插件(Neon 是新的插件)并使用“Build Fat Jar”导出项目。 After that the ´java.lang.ClassNotFoundException´ didn't appear anymore.
之后,“java.lang.ClassNotFoundException”不再出现。
Thanks for your help.谢谢你的帮助。
From Java Document :从Java 文档:
The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols
Class-Path 标头指向本地网络上的类或 JAR 文件,而不是 JAR 文件中的 JAR 文件或可通过 Internet 协议访问的类
If you want to include checkstyle
in the extension jar, you have to explode it into the main jar.如果要在扩展 jar 中包含
checkstyle
,则必须将其分解到主 jar 中。 Take a look at this for more information.看看 这个了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.