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.
Related
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.
NoClassDefFoundError while reading Excel sheet data and printing values
I am getting classnotfound exception for below code :
package practice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class dataDriven {
public static void main(String[] args) throws FileNotFoundException,IOException
{
FileInputStream fis = new FileInputStream("C://Users//484834//testdata.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
wb =null;
int sheets = wb.getNumberOfSheets();
System.out.println("Number of sheets is "+sheets);
}
}
output is :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at practice.dataDriven.main(dataDriven.java:14)
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
It looks like you are missing the the jar file. You can add commons-collections4-x.jar in build path.
URL for the same is -https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.2
Hope this will resolve your issue.
package ReadExcelData;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadandWriteExcel {
public static void main(String []args){
try {
File src = new File("C:\\poi\\ExcelData\\TestExcelData.xlsx");
FileInputStream fis=new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh1= wb.getSheetAt(0);
System.out.println(sh1.getRow(0).getCell(0).getStringCellValue());
System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());
System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());
System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());
System.out.println(sh1.getRow(2).getCell(0).getStringCellValue());
System.out.println(sh1.getRow(2).getCell(1).getStringCellValue());
wb.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Hi,
I Downloaded poi-4.0.0 and added external jar file to my project. After setup of project I created excel file and wrote java program to read from excel file
While run time I am getting error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/zip/ZipFile
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:298)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:307)
at ReadExcelData.ReadandWriteExcel.main(ReadandWriteExcel.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.compress.archivers.zip.ZipFile
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)
... 4 more`enter code here`
You need to add commons-compress-1.18.jar into your project build path.
https://commons.apache.org/proper/commons-compress/download_compress.cgi
Hi guys i've created a saml assertion with opensaml-j and when i run the program an exception occurs and throws NoClassDefFoundError
i've loaded all the necessary jar files but still don't work
here's my code:
package memory;
import java.security.SecureRandom;
import org.joda.time.DateTime;
import org.opensaml.saml.common.SAMLVersion;
import org.opensaml.saml.saml1.core.AttributeStatement;
import org.opensaml.saml.saml1.core.AttributeValue;
import org.opensaml.saml.saml1.core.NameIdentifier;
import org.opensaml.saml.saml1.core.Subject;
import org.opensaml.saml.saml1.core.impl.EvidenceBuilder;
import org.opensaml.saml.saml1.core.impl.NameIdentifierBuilder;
import org.opensaml.saml.saml1.core.impl.SubjectBuilder;
import org.opensaml.saml.saml2.core.Action;
import org.opensaml.saml.saml2.core.Assertion;
import org.opensaml.saml.saml2.core.Attribute;
import org.opensaml.saml.saml2.core.AuthzDecisionStatement;
import org.opensaml.saml.saml2.core.Conditions;
import org.opensaml.saml.saml2.core.DecisionTypeEnumeration;
import org.opensaml.saml.saml2.core.Evidence;
import org.opensaml.saml.saml2.core.Issuer;
import org.opensaml.saml.saml2.core.impl.ActionBuilder;
import org.opensaml.saml.saml2.core.impl.AssertionBuilder;
import org.opensaml.saml.saml2.core.impl.AttributeBuilder;
import org.opensaml.saml.saml2.core.impl.AttributeStatementBuilder;
import org.opensaml.saml.saml2.core.impl.AuthzDecisionStatementBuilder;
import org.opensaml.saml.saml2.core.impl.ConditionsBuilder;
import org.opensaml.saml.saml2.core.impl.IssuerBuilder;
public class CreateSamlAssertion {
#SuppressWarnings("deprecation")
public static Assertion createAssertion(){
Assertion assertion=(Assertion)new AssertionBuilder().buildObject();
assertion.setID(idGenerator());
assertion.setVersion(SAMLVersion.VERSION_20);
assertion.setIssueInstant(new DateTime());
Issuer issuer=(Issuer)new IssuerBuilder().buildObject();
issuer.setNameQualifier("bob");
assertion.setIssuer(issuer);
Subject subject=(Subject)new SubjectBuilder().buildObject();
NameIdentifier nameId=(NameIdentifier)new NameIdentifierBuilder().buildObject();
nameId.setFormat(NameIdentifier.EMAIL);
nameId.setNameIdentifier("alice");
subject.setNameIdentifier(nameId);
Conditions conditions=(Conditions)new ConditionsBuilder().buildObject();
conditions.setNotBefore(new DateTime());
conditions.setNotOnOrAfter(new DateTime().plusMinutes(20));
assertion.setConditions(conditions);
AuthzDecisionStatement authDecisionStatement=getAuthzDecisionStatement();
assertion.getAuthzDecisionStatements().add(authDecisionStatement);
return assertion;
}
private static AuthzDecisionStatement getAuthzDecisionStatement(){
AuthzDecisionStatement aDStatement=(AuthzDecisionStatement)new AuthzDecisionStatementBuilder().buildObject();
DecisionTypeEnumeration decision = DecisionTypeEnumeration.PERMIT;
aDStatement.setDecision(decision);
aDStatement.setResource("http://www.hb.com/Resources/Resource A1");
Action actions=getAction();
actions.setNamespace("http://www.hb.com/Resources/");
aDStatement.getActions().add(actions);
Evidence evidence=getEvidence();
aDStatement.setEvidence(evidence);
return aDStatement;
}
private static Evidence getEvidence(){
Evidence evidence=(Evidence)new EvidenceBuilder().buildObject();
Assertion assertion=(Assertion)new AssertionBuilder().buildObject();
AttributeStatement aStatement=(AttributeStatement) new AttributeStatementBuilder().buildObject();
Attribute attribute1=(Attribute)new AttributeBuilder().buildObject();
attribute1.setName("IssuerCapabilityID");
AttributeValue aValue1=(AttributeValue) AttributeValue.DEFAULT_ELEMENT_NAME;
attribute1.getAttributeValues().add(aValue1);
Attribute attribute2=(Attribute)new AttributeBuilder().buildObject();
attribute2.setName("IssuerCapabilityID");
AttributeValue aValue2=(AttributeValue) AttributeValue.DEFAULT_ELEMENT_NAME;
attribute1.getAttributeValues().add(aValue2);
aStatement.getAttributes().add((org.opensaml.saml.saml1.core.Attribute) attribute1);
aStatement.getAttributes().add((org.opensaml.saml.saml1.core.Attribute) attribute2);
assertion.getAttributeStatements().add((org.opensaml.saml.saml2.core.AttributeStatement) aStatement);
evidence.getAssertions().add(assertion);
return evidence;
}
private static Action getAction(){
Action action=(Action)new ActionBuilder().buildObject();
action.setAction("Read");
action.setAction("Write");
return action;
}
private static String idGenerator(){
SecureRandom random=new SecureRandom();
return String.valueOf(random.nextInt());
}
}
and here's the main class:
package memory;
import org.opensaml.saml.saml2.core.Assertion;
public class SamlTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Assertion assertion=CreateSamlAssertion.createAssertion();
System.out.println(assertion);
}
}
the stack trace
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at org.opensaml.core.xml.AbstractXMLObject.<init>(AbstractXMLObject.java:47)
at org.opensaml.xmlsec.signature.AbstractSignableXMLObject.<init>(AbstractSignableXMLObject.java:47)
at org.opensaml.saml.common.AbstractSignableSAMLObject.<init>(AbstractSignableSAMLObject.java:44)
at org.opensaml.saml.saml2.core.impl.AssertionImpl.<init>(AssertionImpl.java:83)
at org.opensaml.saml.saml2.core.impl.AssertionBuilder.buildObject(AssertionBuilder.java:45)
at org.opensaml.saml.saml2.core.impl.AssertionBuilder.buildObject(AssertionBuilder.java:40)
at memory.CreateSamlAssertion.createAssertion(CreateSamlAssertion.java:34)
at memory.SamlTest.main(SamlTest.java:9)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 8 more
and thanks in advance
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