java.lang.NoClassDefFoundError:在eclipse maven中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18670749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoClassDefFoundError: in eclipse maven
提问by Prashant Aggarwal
In eclipse with maven, I have add a dependency as a local jar file, as like this:
在使用 Maven 的 Eclipse 中,我添加了一个依赖项作为本地 jar 文件,如下所示:
<dependency>
<groupId>xyz-core</groupId>
<artifactId>xyz-core</artifactId>
<version>0</version>
<scope>system</scope>
<systemPath>/home/xyz/xyz-core.jar</systemPath>
</dependency>
In this jar file I have a interface that is using in my application.
在这个 jar 文件中,我有一个在我的应用程序中使用的接口。
When I run my application on tomcat server It show exception for that interface
当我在 tomcat 服务器上运行我的应用程序时它显示该接口的异常
Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: com/mxgraph/canvas/mxICanvas2D
while mxICanvas2D
is a interface.
whilemxICanvas2D
是一个接口。
回答by Jesper
This is most likely because you have set the scope to system
. According to the Maven documentation:
这很可能是因为您已将范围设置为system
。根据Maven 文档:
system
This scope is similar to
provided
except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
系统
此范围类似于
provided
除了您必须提供明确包含它的 JAR 之外。该工件始终可用,不会在存储库中查找。
In other words, the dependency is not put on your classpath when you run your application if you use system
; you have to do that yourself.
换句话说,如果您使用system
;运行应用程序时,依赖项不会放在您的类路径上。你必须自己做。
Use one of the other scopes, for example compile
.
使用其他范围之一,例如compile
。
回答by Debojit Saikia
Have you added "Maven Dependencies" to the project's "Web Deployment Assembly". If not, add that as follows:
您是否将“Maven 依赖项”添加到项目的“Web 部署程序集”中。如果没有,请按如下方式添加:
Right Click on your project -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Next and then from there you can add "maven Dependencies". Then build and try to run your app.
右键单击您的项目 -> 属性 -> 部署程序集 -> 添加 -> Java 构建路径条目 -> 下一步,然后您可以从那里添加“maven 依赖项”。然后构建并尝试运行您的应用程序。
回答by porfiriopartida
Since you are using system scope it means that maven will use that to compile your project, you will not see your errors, however, when you are running your application in tomcat, that is not related to maven and tomact does not know where your dependencies are, one way to solve it is copy the needed .jars to your tomcat/libfolder.
由于您使用的是系统范围,这意味着 maven 将使用它来编译您的项目,您不会看到您的错误,但是,当您在 tomcat 中运行您的应用程序时,这与 maven 无关,tomact 不知道您的依赖项在哪里是,解决它的一种方法是将所需的 .jars 复制到您的 tomcat/lib文件夹。
Usually you will like to have provided or compile scope, however these scopes are used if you have a repository. When you create a new mavenized project and then build it, maven will create a .jar for that particular dependency in your local machine (c:/users/user/.m2/repository/). That will work for your own projects.
通常,您希望提供或编译范围,但是如果您有存储库,则会使用这些范围。当您创建一个新的 mavenized 项目然后构建它时,maven 将在您的本地机器 (c:/users/user/.m2/repository/) 中为该特定依赖项创建一个 .jar。这将适用于您自己的项目。