OpenCSV classes not defined with maven even though dependancy is clarified

84 Views Asked by At

So I'm trying to use OpenCSV with my maven project and it works before packaging (Builds no problem and I can see the classes are present) but I keep getting this error with Java that OpenCSV classes aren't defined when running the package:

java.lang.NoClassDefFoundError: com/opencsv/CSVReader
    ...
Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVReader
    ...

Here's my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>game</groupId>
    <artifactId>Plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>24.0.1</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.14.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.9</version>
        </dependency>
    </dependencies>
</project>

I'm using IntelliJ as my IDE and I have no idea why this doesn't work since I've never had this issue before. I also tried to make a new project and same result each time

1

There are 1 best solutions below

0
Roy503 On

When you package Maven it won't package the dependencies into the jar, you need to either move them to where you're running it or create a fat jar like this:

clean compile assembly:single