2017-10-06 104 views
1

我得到這個例外的NoClassDefFoundError脫穎而出

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException at com.restcalls.JSONtoCSV.main(JSONtoCSV.java:176)

當我試圖將csv文件轉換爲excel文件。 此行似乎是導致問題 - XSSFWorkbook workBook = new XSSFWorkbook();

我已經添加了最新的POI jar - 3.17,這是我的代碼:

String xlsxFileAddress = "C:/Users/xxxxx/REST/exports/test.xlsx"; //xlsx file address 
System.out.println("here 0"); 
XSSFWorkbook workBook = new XSSFWorkbook(); 
System.out.println("here 0.1"); 
XSSFSheet sheet = workBook.createSheet("sheet1"); 
System.out.println("here 1"); 
String currentLine=null; 
int RowNum=0; 
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress)); 
System.out.println("here 2: "+br); 
while ((currentLine = br.readLine()) != null) { 
    String str[] = currentLine.split(","); 
    RowNum++; 
    XSSFRow currentRow=sheet.createRow(RowNum); 
    for(int i=0;i<str.length;i++){ 
     currentRow.createCell(i).setCellValue(str[i]); 
    } 
} 

FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress); 
workBook.write(fileOutputStream); 
fileOutputStream.close(); 
System.out.println("Done"); 
+0

https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.5。當你搜索類的maven依賴時,0是第一個發現。 – chillworld

+0

@chillworld謝謝!我添加了xmlbeans-2.5.0 jar和poi-ooxml-schemas jar,但是現在我得到了下面的異常:線程「main」中的異常java.lang.NoClassDefFoundError:org/apache/poi/POIXMLTypeLoader \t at org.openxmlformats.schemas.spreadsheet.xml.x2006.main.CTWorkbook $ Factory.newInstance(Unknown Source) \t at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:248) –

+0

https:// mvnrepository.com/artifact/org.apache.poi/poi-ooxml =>只要去谷歌,複製你的類到谷歌搜索並添加mvn。通常情況下(例外情況存在),你將在maven中獲得正確的依賴關係,只需選擇正確的版本 – chillworld

回答

0

你需要立即下載並添加XML豆依賴到類路徑。該庫通常被稱爲xmlbeans-x.x.x.jar。例如:搜索和下載的xmlbeans-2.6.0.jar或者如果你有一個Maven項目設置POM如下

<dependency> 
    <groupId>org.apache.xmlbeans</groupId> 
    <artifactId>xmlbeans</artifactId> 
    <version>2.6.0</version> 
    </dependency> 
+0

我不是在做一個Maven項目,它是一個簡單的java項目。我需要將其轉換爲Maven項目嗎? –

+0

不,您不需要將其轉換爲Maven項目。只需下載jar並將其添加到您的課程路徑。 – Eritrean

+0

謝謝!我已經添加了2.5.0 jar,沒有得到這個異常 –

相關問題