NoClassDefFoundError in Apache POI/FileOutputStream - java

I get NoClassDefFoundError error when I try run below code. I checked tons of similar posts, but it didn't helped. What do I wrong? I think, there is something with FileOutputStream but I don't know what.
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Vienna");
File viennaBatch = new File("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
viennaBatch.createNewFile();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("a");
FileOutputStream fileOut
= new FileOutputStream("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
workbook.write(fileOut);
fileOut.close();
}
}```
error:
```Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:99)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:121)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1357)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 8 more```

Quote from the POI page:
Apache commons-math3 and commons-compress were added as a dependency
in POI 4.0.0. Zaxxer SparseBitSet was added as a dependency in POI
4.1.2
The exception you're getting indicates that commons-math3 is missing in the classpath.

Related

can't find apache poi libraries

i'm trying for the first time the apache poi library to manage Execel files. I follow this steps to import apache poi: Import Apache POI in Intellij for JAVA.
The code compile without error but when i run it give me this error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at Test.main(Test.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 1 more
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\Huawei\\IdeaProjects\\untitled\\src\\TEST.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while(itr.hasNext()){
Row row = itr.next();
Iterator<Cell> celliterator = row.cellIterator();
while(celliterator.hasNext()){
Cell cell = celliterator.next();
switch (cell.getCellType()){
case STRING: //field that represents string cell type
System.out.print(cell.getStringCellValue() + "\t\t\t");
break;
case NUMERIC: //field that represents number cell type
System.out.print(cell.getNumericCellValue() + "\t\t\t");
break;
default:
System.out.println("NOT NUMERIC AND NOT STRING");
}
}
}
}catch (Exception e ){
e.printStackTrace();
}
}
}
This seems like a classic Maven Project with a .jar downloaded not installed via mvn command.
If ClassNotFoundException raises in runtime, its because your project can't find the library after compiled.
Is this the case? If so, you must add the .jar via mvn install and declare him in your pom.xml. A better solution it's just adding the maven dependency directly:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>

i get ClassNotFoundException when i try to read from excel file

When i try to read from excel file using apace poi i get the ClassNotFoundException followed by other errors and i have imported all the necessary jar files in the reference library
by the way i'm still new to coding
heres the code :
import java.io.*;
import java.io.File;
import java.io.FilterInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.*;
import org.apache.poi.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream F = new FileInputStream("Carbcounting.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(F);
XSSFSheet sheet = wb.getSheetAt(0);
FormulaEvaluator formulaEva = wb.getCreationHelper().createFormulaEvaluator();
for(Row row : sheet){
for(Cell cell : row){
System.out.print(cell.getStringCellValue());
}
}
System.out.println();
}
}
and heres all the errors i'm getting when i try to run it :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at project.Test.main(Test.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
You need to Add commons-collections4-x.x.jar file in your build path and try it again. It should work.
Get it from here: https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.0
Also, just adding up:
You are getting this error(NoClassDefFoundError) majorly for two reasons:
Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.
If a class was present during compile time but not available in java classpath during runtime.

Read excel file in Java with NetBeans

I need to read an excel file. I wrote this code but it has an error.
Here is my code:
package javaapplication9;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.*;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try{
//File fileName = new File("C://sadegh//test.xlsx");
InputStream inp = new FileInputStream("C://sadegh//test.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
System.out.println(cell.toString());
}catch(java.lang.NullPointerException e5){
//e5.notify();
}catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e3) {
e3.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
This is the error as shown in the StackTrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.apache.poi.openxml4j.opc.Package.<clinit>(Package.java:63)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:62)
at javaapplication9.Main.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 3 more
Java Result: 1
What are the causes of the error?
Here are my libs imported. I'm using:
xbean.jar
xbean_xpath.jar
xmlbeans-qname.jar
dom4j-1.6.1.jar
poi-ooxml-3.8-20120326.jar
poi_ooxml-schemas-3.8-20120326.jar
poi-3.8-20120326.jar
You need the log4 jar as lib in the classpath
Your Apache POI requires Log4J 1.2.13
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
You can download it here: http://central.maven.org/maven2/log4j/log4j/1.2.13/log4j-1.2.13.jar

Excel 2007 file corrupted after being created in Apache POI

I use NetBeans 8. I got problem after compiling this simple code:
package file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
public class File {
public static void main(String[] args) throws FileNotFoundException, IOException
{ Workbook wb = new XSSFWorkbook();
String name = "charlie.xlsx";
FileOutputStream fileOut = new FileOutputStream(name);
wb.write(fileOut);
fileOut.close();
I'm total rookie in Java so basically I rewrote that code from Apache POI documentation, trying to understand how it works. Well - it works fine until I try to open the output file in MS Excel - because then I get a message that file cannot be open cause it's corrupt.
What went wrong?
You need to create a Sheet. Add this to your code and it will work.
wb.createSheet("Test1");

opening excel ooxml 2007 xlsx document returns nullPointer exception

How do I open a 2007 xlsx ooxml file with Apache POI? I've added everything to my class path but keep getting this java.lang.NullPointerException error:
"C:\Program Files\Java\jdk1.7.0_13\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:1804,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_13\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\zipfs.jar;C:\Users\Patrick\Desktop\xmlChanger\out\production\xmlChanger;C:\poi-3.9\poi-3.9-20121203.jar;C:\poi-3.9\poi-examples-3.9-20121203.jar;C:\poi-3.9\poi-excelant-3.9-20121203.jar;C:\poi-3.9\poi-ooxml-3.9-20121203.jar;C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;C:\poi-3.9\poi-scratchpad-3.9-20121203.jar;C:\poi-3.9\lib\commons-codec-1.5.jar;C:\poi-3.9\lib\commons-logging-1.1.jar;C:\poi-3.9\lib\junit-3.8.1.jar;C:\poi-3.9\lib\log4j-1.2.13.jar;C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;C:\poi-3.9\ooxml-lib\stax-api-1.0.1.jar;C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1\lib\idea_rt.jar" Main
Connected to the target VM, address: '127.0.0.1:1804', transport: 'socket'
java.lang.NullPointerException
at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:625)
at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:91)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:56)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)
at Main.main(Main.java:178)
Disconnected from the target VM, address: '127.0.0.1:1804', transport: 'socket'
Process finished with exit code 0
My code is as follows:
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
try {
File file = new File(root_dir + "2013-03-13 iom diff.xlsx");
Workbook workbook = WorkbookFactory.create(file); //fails here
//...
} catch (Exception e) {
e.printStackTrace();
}
}
}
Again I have searched already for this but haven't found any answers that worked (e.g. making sure the directory path is correct, going out of bounds etc.)
I asked a similar question before but the error was different because I believe I was using just the normal xlsx format and not the OOXML xlsx format when saving in Microsoft Excel.
Any ideas where this null pointer exception is coming from or a way of making the exception thrown more meaningful?
EDIT
When I use the following code to create a workbook, it opens with the code posted above:
Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row r = sheet.createRow((short) 0);
Cell cell = r.createCell(0);
cell.setCellValue(1);
r.createCell(1).setCellValue(1.2);
r.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));
r.createCell(3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
This implies an invalid format but I'm not sure what the deal is because I saved as OOXML in Excel

Categories