Maven依赖项上的NoClassDefFoundError [英] NoClassDefFoundError on Maven dependency

查看:81
本文介绍了Maven依赖项上的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Maven并且我遇到了依赖关系。

My first use of Maven and I'm stuck with dependencies.

我使用Eclipse创建了一个Maven项目并添加了依赖项,并且它没有问题。

I created a Maven project with Eclipse and added dependencies, and it was working without problems.

但是当我尝试通过命令行运行它:

But when I try to run it via command line:

$ mvn package  # successfully completes
$ java -cp target/bil138_4-0.0.1-SNAPSHOT.jar tr.edu.hacettepe.cs.b21127113.bil138_4.App # NoClassDefFoundError for dependencies

它下载依赖项,成功构建,但是当我尝试运行它时,我得到NoClassDefFoundError:

It downloads dependencies, successfully builds, but when I try to run it, I get NoClassDefFoundError:

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonParseException
        at tr.edu.hacettepe.cs.b21127113.bil138_4.db.DatabaseManager.<init>(DatabaseManager.java:16)
        at tr.edu.hacettepe.cs.b21127113.bil138_4.db.DatabaseManager.<init>(DatabaseManager.java:22)
        at tr.edu.hacettepe.cs.b21127113.bil138_4.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.JsonParseException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        ... 3 more

我的pom.xml是这样的:

My pom.xml is like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>tr.edu.hacettepe.cs.b21127113</groupId>
  <artifactId>bil138_4</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bil138_4</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>        
    <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>           
    </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>             
        </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.6</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.6</version>
    </dependency>
    </dependencies>
  </dependencyManagement>
</project>

任何人都可以帮助我吗?

Can anyone help me?

推荐答案


当我尝试运行它时,我得到NoClassDefFoundError

when I try to run it, I get NoClassDefFoundError

运行怎么样?你可能试图在没有正确导入maven类路径的情况下使用eclipse运行它。请参阅 m2eclipse 插件,以便将maven与eclipse集成。

Run it how? You're probably trying to run it with eclipse without having correctly imported your maven classpath. See the m2eclipse plugin for integrating maven with eclipse for that.

To验证您的maven配置是否正确,您可以使用 exec插件运行您的应用程序:

To verify that your maven config is correct, you could run your app with the exec plugin using:

mvn exec:java -D exec.mainClass=<your main class>

更新:首先,关于运行 exec:java ,您的主类是 tr.edu.hacettepe.cs.b21127113.bil138_4.App 。在谈论类名时,它们(几乎)总是以点分隔。简单的类名只是最后一部分: App 在你的情况下。完全限定的名称是完整的包加上简单的类名,这是您在运行某些内容时为maven或java提供的名称。您尝试使用的是源文件的文件系统路径。那是一个完全不同的野兽。与文件系统中的源文件相比,类名通常直接转换为类路径中的类文件。在您的特定情况下,有问题的类文件可能位于 target / classes / tr / edu / hacettepe / cs / b21127113 / bil138_4 / App.class ,因为maven编译为 target / classes ,java传统上为每个包装级别创建一个目录。

Update: First, regarding your error when running exec:java, your main class is tr.edu.hacettepe.cs.b21127113.bil138_4.App. When talking about class names, they're (almost) always dot-separated. The simple class name is just the last part: App in your case. The fully-qualified name is the full package plus the simple class name, and that's what you give to maven or java when you want to run something. What you were trying to use was a file system path to a source file. That's an entirely different beast. A class name generally translates directly to a class file that's found in the class path, as compared to a source file in the file system. In your specific case, the class file in question would probably be at target/classes/tr/edu/hacettepe/cs/b21127113/bil138_4/App.class because maven compiles to target/classes, and java traditionally creates a directory for each level of packaging.

你原来的问题只是那个你还没有将杰克逊的杰克放在你的课堂上。从命令行运行java程序时,必须设置类路径,让它知道从哪里加载类。你已经添加了自己的jar,但没有添加其他所需的jar。您的评论让我觉得您不了解如何手动构建类路径。简而言之,类路径可以有两个东西:包含类文件的目录和包含类文件的jar。包含jar的目录不起作用。有关构建类路径的更多详细信息,请参阅设置类路径 java javac 工具文档。

Your original problem is simply that you haven't put the Jackson jars on your class path. When you run a java program from the command line, you have to set the class path to let it know where it can load classes from. You've added your own jar, but not the other required ones. Your comment makes me think you don't understand how to manually build a class path. In short, the class path can have two things: directories containing class files and jars containing class files. Directories containing jars won't work. For more details on building a class path, see "Setting the class path" and the java and javac tool documentation.

您的课程路径需要至少,并且没有换行符:

Your class path would need to be at least, and without the line feeds:

target/bil138_4-0.0.1-SNAPSHOT.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.9.6/jackson-core-asl-1.9.6.jar:
/home/utdemir/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.9.6/jackson-mapper-asl-1.9.6.jar

请注意,Windows上的分隔符是分号(;)。

Note that the separator on Windows is a semicolon (;).

我很抱歉没有及早发现它。问题出在你原来的帖子里,但我错过了。

I apologize for not noticing it sooner. The problem was sitting there in your original post, but I missed it.

这篇关于Maven依赖项上的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆