问题
I've added the open csv jar
to my project to enable data to be written out to file in csv format.
The jar file was added using the following steps:
1.Properties --> Add external jars --> opencsv-3.1.jar
2.Order & Eport tab --> tick, opencsv-3.1.jar
But when I run the project I get an error stating that one of the methods belonging to the opencsv jar cannot be found: java.lang.NoClassDefFoundError: com.opencsv.CSVWriter
Does anyone know how to resolve this error or am I missing some step in adding the jar to the project?
`
回答1:
See javadoc of API
CSVWriter is in au.com.bytecode.opencsv package
回答2:
As cross-listed from here, this was my solution to the problem:
I have been struggling with getting OpenCSV set up with Maven and eclipse for a while due to exactly the same error. Ultimately I abandoned OpenCSV and used CSVParser instead, which is available from the Apache Commons and works much more readily.
Update your POM with the dependency listed here, and the following will work out-of-the-box:
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.Reader;
public class importFile {
public static void main(String[] args) {
Reader in = new FileReader( csvFileInput );
CSVParser parser = new CSVParser( in, CSVFormat.DEFAULT );
List<CSVRecord> list = parser.getRecords();
for( CSVRecord row : list )
for( String entry : row )
System.out.println( entry );
}
}
来源:https://stackoverflow.com/questions/27770584/noclassdeffounderror-after-adding-opencsv-jar-to-project