Getting error while reading xlsx file through apche poi - java

import java.io.File;
import java.io.FileInputStream;
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 Exx {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("D:\\eXCEL.xlsx"));
//WorkbookFactory wrk1 = WorkbookFactory.create(fis)
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator ite = sheet.rowIterator();
while(ite.hasNext()){
Row row = (Row) ite.next();
Iterator<Cell> cite = row.cellIterator();
while(cite.hasNext()){
Cell c = cite.next();
System.out.print(c.toString() +" ");
}
System.out.println();
}
fis.close();
}
}
This is my source code.
JAR Files used are
poi-3.10-FINAL.jar
poi-examples-3.10.jar
poi-excelant-3.10.jar
poi-ooxml-3.10.jar
poi-scratchpad-3.10-final.jar
xmlbean-2.3.0.jar
dom4j1.6 jar
while running getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at Exx.main(Exx.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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
Please help to sort it out. I tried all beans and all, yet also getting error.

The code runs fine if the below jars are included in the classpath:
dom4j-1.6.1.jar
poi-3.10-FINAL.jar
poi-ooxml-3.10-FINAL.jar
poi.ooxml-schemas-3.10-FINAL.jar
xmlbeans-2.3.0.jar

Related

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException

I have the following jar files
dom4j-1.6.1.jar
poi-3.11.jar
poi-ooxml-3.11.jar
poi-ooxml-schemas-3.15-beta2.jar
xmlbeans-2.6.0.jar
poi-scratchpad-3.11.jar
poi-excelant-3.11.jar
and i'm trying to read an excel(2007) worksheet. My code is as follows
import java.io.File;
import java.io.FileInputStream;
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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class AdaptiveControllers {
public static void main(String[] args) throws IOException {
String excelFilePath = "test.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
}
System.out.print(" - ");
}
System.out.println();
}
workbook.close();
inputStream.close();
}
}
but when I run the code in eclipse I get the following exception
Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:427)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:162)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:263)
at AdaptiveControllers.main(AdaptiveControllers.java:17)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
... 4 more
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/POIXMLTypeLoader
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:46)
... 9 more
Caused by: java.lang.ClassNotFoundException: org.apache.poi.POIXMLTypeLoader
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 11 more
What am I doing wrong? Could anyone please help me?

Java reading an Excel file

I believe i have all the jar files I need and i'm still getting an error that says Caused by:
java.lang.reflect.InvocationTargetException
And forgive me, i'm still not wonderful at reading error messages. The error suggests the issue is with the line "Workbook book = new XSSFWorkbook(stream);"
import java.io.File;
import java.io.FileInputStream;
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.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class excelReader {
public static void main(String[] args) throws IOException{
String spreadSheetFilePath = "C:\\Users\\userExample\\Documents\\forJava.xlsx";
FileInputStream stream = new FileInputStream(new File(spreadSheetFilePath));
Workbook book = new XSSFWorkbook(stream);
Sheet sheetOne = book.getSheetAt(0);
Iterator<Row> iterator = sheetOne.iterator();
while(iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIter = nextRow.cellIterator();
while(cellIter.hasNext()) {
Cell cell = cellIter.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
}
System.out.print(" - ");
}
System.out.println();
}
book.close();
stream.close();
}
}
Error i'm getting
Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:279)
at excelReader.main(excelReader.java:18)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
... 4 more
Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLTypeLoader.<clinit>(POIXMLTypeLoader.java:50)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)
... 10 more
Jar files in my Library for project:
commons-codec-1.10.jar
commons-logging1-2.jar
dom4j-1.1.jar
junit-4.1.2.17.jar
poi-3.14-20160307.jar
poi-excelant-3.14-20160307.jar
poi-ooxml-schemas-3.14-20160307.jar
poi-scratchpad-3.14-20160307.jar
xmlbeans-2.3.0.jar
If you look at the StackTrace you can find that the exception is caused by :
java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
based on that I think you need the : xmlbeans-xmlpublic-2.6.0.jar inside your class path, try to add this additional jar into the class path and try again ...
I suggest to manage the dependency using Maven, that will simplify everything, just switch to maven and add the following lines to your pom file :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

package photo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
public class capture {
public static void main(String arg[]) throws IOException {
Webcam webcam = Webcam.getDefault();
webcam.open();
BufferedImage image = webcam.getImage();
ImageIO.write(image, "JPG", new File("test.jpg"));
}
}
I have written this java code to capture photo through webcam, but this code is throwing an error/exception please tell how can I fix it.?
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at photo.capture.main(capture.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 2 more
Download SLF4J and put the slf4j-api-1.7.2.jar file onto your class path.

Getting ClassNotFoundException while using Class.forName("com.actitime5.Testcases."+vriable);

I've been messing around with ClassLoaders in java recently, trying to run my automation script code which uses dynamic loading of classes and geting ClassNotFoundException.
##MyCode
package com.actitime5.frameworkengine;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import com.gmail5.Testcases.LoginLogout;
import org.junit.runner.JUnitCore;
public class Init {
public static void main(String args[]) throws Exception
{
Init in=new Init();
String url=in.getConfigValue("url");
ExcelLibrary lib=new ExcelLibrary();
JUnitCore core = new JUnitCore();
int numberofTestcases=lib.getRowCont("Scenario");
System.out.println(numberofTestcases);
for(int i=1;i<=numberofTestcases;i++)
{String status=lib.getExcelData("Scenario", i, 1);
if(status.equals("yes"))
{String scriptnam=lib.getExcelData("Scenario", i, 0);
System.out.println("Sheet Name"+" "+scriptnam);
Class scriptToRun= Class.forName("com.gmail5.Testcases."+scriptnam);
core.run(scriptToRun);
}
}}
##Errors that i am getting :
1
Sheet Name LoginLogout
Exception in thread "main" java.lang.ClassNotFoundException:com.gmail5.Testcases.LoginLogout
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.gmail5.frameworkengine.Init.main(Init.java:24)
The class which I am trying to load is available in package: com.gmail5.Testcases.*;,but still it throws exception
I think your class name has trailing whitespace. Try trim()ming it off before attempting to load the class.
You can confirm this by replacing
System.out.println("Sheet Name"+" "+scriptnam);
with
System.out.println("Sheet Name"+" '"+scriptnam+"'");
I expect this will print out something like the following:
Sheet Name 'LoginLogout '
When I clicked 'edit' to edit your question, I could see there was trailing whitespace after the class name in the stack trace and in your Sheet Name message. (I didn't change anything in your question: I just used the Edit function to see the raw text of your question, and then clicked Cancel.)

Getting NoClassDef Error for classes imported through Maven

I have a Maven project in Eclipse created through m2e. It obtains client libraries from a project on GitHub called alternator. The classes seem to be imported fine, in my project, but on running them, it shows a NoClassDefFound error. Here's the code and the error:
Code:
public class Main{
private AlternatorDBClient client;
private DynamoDBMapper mapper;
private AlternatorDB db;
public static void main(String args[]) throws Exception{
new Main().run();
}
public void run() throws Exception {
this.client = new AlternatorDBClient();
this.mapper = new DynamoDBMapper(this.client);
this.db = new AlternatorDB().start();
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/michelboudreau/alternator/AlternatorDBClient
at Main.run(Main.java:17)
at Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: com.michelboudreau.alternator.AlternatorDBClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 2 more
The AlternatorDBClient class is importing one or more classes which are not on your class path:
import com.amazonaws.*;
import com.amazonaws.handlers.HandlerChainFactory;
import com.amazonaws.http.ExecutionContext;
import com.amazonaws.http.HttpResponseHandler;
import com.amazonaws.http.JsonErrorResponseHandler;
import com.amazonaws.http.JsonResponseHandler;
import com.amazonaws.services.dynamodb.AmazonDynamoDB;
import com.amazonaws.services.dynamodb.model.*;
import com.amazonaws.services.dynamodb.model.transform.*;
import com.amazonaws.transform.JsonErrorUnmarshaller;
import com.amazonaws.transform.JsonUnmarshallerContext;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.util.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
You are somehow missing:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
</dependency>
as a dependency in your project pom. As AlexR stated, please post your pom.

Categories