I am getting this error: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat

huangapple go评论59阅读模式
英文:

I am getting this error: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat

问题

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.8</version>
    </dependency>
</dependencies>

出现这个错误的原因我不清楚,每当我尝试运行我的 Maven 项目时,都会出现这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat.

但我已经检查了我的类路径,它是正确的。我也检查过了我的依赖,它们也是正确的。

我认为可能是执行命令导致了这个异常。在类路径中如何运行带有外部 JAR 的 Maven 项目呢?

java -cp target/classes 包名

这样是正确的吗?

英文:
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.8</version>
    </dependency>
</dependencies>

For some reason whenever I try to run my maven project I get this error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat.

But I checked my classpath, it's correct. I've checked my dependencies, they're correct.

I think it might be the command that is leading to the exception. How do you run a Maven project with an external jar in the classpath?

java -cp target/classes packageName 

Is that correct?

答案1

得分: 0

> 如何在类路径中运行带有外部 JAR 的 Maven 项目?

> java -cp target/classes 包名

您的类路径需要包括代码所依赖的库的 JAR 文件。例如:

 java -cp target/classes:path/to/commons-csv-1.8.jar 包名

如果 commons-csv 有运行时依赖关系,您还需要包括它们的 JAR 文件。(但根据其 POM 文件,它似乎没有...)


另外,您还可以使用 mvn exec:java ... 来运行您的应用程序,详细信息请参阅这个 StackOverflow 的问答:https://stackoverflow.com/questions/1089285/maven-run-project。

英文:

> How do you run a maven project with an external jar in the classpath?

> java -cp target/classes packageName

Your classpaths need to include the JAR files for the libraries that your code depends on. For example:

 java -cp target/classes:path/to/commons-csv-1.8.jar packageName

If commons-csv had runtime dependencies you would need to include the JAR files for them as well. (But it doesn't ... according to its POM file.)


Alternatively, you could use mvn exec:java ... to run your application as described in this StackOverflow Q&A: https://stackoverflow.com/questions/1089285/maven-run-project.

huangapple
  • 本文由 发表于 2020年7月26日 16:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63097896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定