我试图使用opencsv获得加密格式的数据。但是在写CSV作者的时候,我得到了"NoClassDefFoundError“。我见过很多与同样错误有关的帖子,几乎每件事都试过了,但仍然无法纠正问题。下面链接有相同的错误。
CSVHelper代码:
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();
}

所有打开csv库的东西在我的libs文件夹里。
错误日志:
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)如果需要任何其他细节,请告诉我。任何帮助都将不胜感激。
我还要补充的另一件事是,在得到上面的异常之前,下面是错误。
无法找到从方法'com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy',引用的类com.example.encryption.CSVHelper.parse
发布于 2015-04-14 12:16:03
它为其提供错误的类可能存在于类路径中,但您需要的jar可能有一些依赖关系。
请查找下面的链接,以检查依赖项并添加依赖jars:http://opencsv.sourceforge.net/dependencies.html
发布于 2017-01-08 21:55:13
由于完全相同的错误,我一直在为Maven和eclipse设置OpenCSV而挣扎一段时间。最终,我放弃了OpenCSV,使用了CSVParser代替,它可以从Apache中获得,而且工作起来更加容易。
使用依赖项列在这里更新您的POM,下面的操作将在开箱即用:
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/29626457
复制相似问题