0

I am trying to use the opencsv library in Eclipse for a Java project. I downloaded opencsv-5.7.1.jar from here and added it under "referenced libraries" in my project folder. When I run the program, I'm getting a NoClassDefFoundError for CSVReader. Any thoughts? Wondering if I need to save the opencsv jar in a different part of the project, or do something to show the source code to eclipse as suggested in this response. Apologies if this is a basic question, I'm new at all this. Thanks!

Here is the jar's location in the project folder. I'm trying to use it in the CSVReaderTest class. jar file under referenced libraries

and here's some test code I've been using to try to open the reader.

package com.usf.a2;

import java.util.Arrays;
import com.opencsv.*;
import java.io.FileReader;

public class CSVReaderTest {
    public static void main(String[] args) {
        try {
            CSVReader reader = new CSVReader(new FileReader("test.csv"));
            String[]nextline;
            while((nextline=reader.readNext()) != null) {
                if (nextline != null) {
                    System.out.println(Arrays.toString(nextline));
                }
            }
        }
            
        catch(Exception e) {
            System.out.println(e);
        }
        System.out.println("CSV Read complete");        
    }
}

I've also tried:

com.opencsv.CSVReader reader = new com.opencsv.CSVReader(FileReader("test.csv"));

But either way I get these error messages:

Exception in thread "main" java.lang.NoClassDefFoundError: com/opencsv/CSVReader
    at com.usf.a2.CSVReaderTest.main(CSVReaderTest.java:10)
Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVReader
    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:520)
    ... 1 more
  • Try running with `java -verbose:class` to check if class is initialized or there's some error. Even for simple projects a dependecy manager is recommended. – LMC Apr 01 '23 at 21:53

0 Answers0