NoClassDefFoundError and ClassNotFoundException when reading data from a csv file using opencsv in java

32 Views Asked by At

im making an application to automate sending emails. Initially the csv file is read, and an instance of a Recipient class is made and that is added to an arraylist. However, i am getting a load of errors, including NoClassDefFoundError and ClassNotFoundException.

Here is my code:

import java.util.ArrayList;
import java.util.List;
import java.io.FileReader;
import java.io.IOException;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;


public class UserDataLoader {

    private final String url;
    private ArrayList<RECIPIENT> mailingList = new ArrayList<>();

    public UserDataLoader (String url){
        this.url = url;
    }

    public void loadData(){
        try (CSVReader reader = new CSVReader(new FileReader(url))) {
            List<String[]> records = reader.readAll();

            for (int i = 1; i <= records.size(); i++){
                String[] record = records.get(i);
                String name = record[0];
                String emailAddress = record[1];
                String dateOfBirth = record[2];

                RECIPIENT customer = new RECIPIENT(name, emailAddress, dateOfBirth);
                mailingList.add(customer);
    
            }
        
        } catch (IOException | CsvException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<RECIPIENT> getMailinList(){
        return mailingList;
    }


}

Here are the exact errors: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/ObjectUtils at com.opencsv.CSVParser.(CSVParser.java:110) at com.opencsv.CSVReader.(CSVReader.java:99) at UserDataLoader.loadData(UserDataLoader.java:19) at App.main(App.java:5) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.ObjectUtils at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525) ... 4 more

I should have expected no errors and am unsure where this has come from. I have downloaded opencsv, placed it into my referenced libraries in vscode, and all of the java files and the csv file are in the same folder.

1

There are 1 best solutions below

0
nicolas van On

add it in your pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>