-1
<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?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

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: Maven Run Project.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216