简体   繁体   中英

NoClassDefFoundError: com.opencsv.CSVWriter

I am try to use opencsv libray to get encrypted format data. But while writing on CSV writer I am getting "NoClassDefFoundError". I have seen many post related with same error and tried also almost every thing, but still not able to rectify the problem. Below link with same error.

csv description1

csv description2

CSVHelper code:

public String stringify(List<? extends SerializeableToCSV> objects) 
        throws IllegalArgumentException {

    if (objects == null || objects.isEmpty()) 
        throw new IllegalArgumentException("List of objectes passed is either null or empty");

    List<String[]> strList = new ArrayList<String[]>();

    // add header record
    strList.add(objects.get(0).getColumnHeaders());

    Iterator<? extends SerializeableToCSV> it = objects.iterator();
    while (it.hasNext()) {
        SerializeableToCSV element = it.next();
        strList.add(element.getValueList());
    }

    StringWriter writer = new StringWriter();
    CSVWriter csvWriter = new CSVWriter(writer, CSVHelper.VALUE_SEPERATOR, CSVWriter.NO_QUOTE_CHARACTER);
    csvWriter.writeAll(strList);

    try {
        csvWriter.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return writer.toString();
}

在此处输入图片说明

All open csv library stuff in my libs folder.

Error LOG:

04-14 16:03:09.260: E/AndroidRuntime(13047): java.lang.NoClassDefFoundError: com.opencsv.CSVWriter
04-14 16:03:09.260: E/AndroidRuntime(13047):    at com.stellapps.encryption.CSVHelper.stringify(CSVHelper.java:62)
04-14 16:03:09.260: E/AndroidRuntime(13047):    at com.stellapps.encryption.Csv.createCSVString(Csv.java:62)
04-14 16:03:09.260: E/AndroidRuntime(13047):    at com.stellapps.encryption.Csv.generateCsvEncryptedFormat(Csv.java:111)

Please let me know if any other details required. Any help will be appreciated.

One more thing I want to add is, below error before getting the above exception.

Could not find class 'com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy', referenced from method com.example.encryption.CSVHelper.parse

The class for which it is giving error might be present in the class path but the jar which you require might have some dependencies.

Please find the link below to check the dependencies and add the dependent jars: http://opencsv.sourceforge.net/dependencies.html

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 );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM