Java reading an Excel file - java

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>

Related

org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org.testng.TestNG?

I have project in eclipse it's an WebService Project REST API's and i have also added another project in Configure Build path -> Add Project and i have added one another Project in my API method i am calling TestNGCreator class look below.
Basically this project uses jersey framework in java creating REST API and when i am calling any perticular method i.e. execute it will call another class which resides in another project i have added project in Build path of this project my other methods are working fine but when i am calling method from different project i having this error.
Do i need to declare some where that i am using this class and this method in POM.xml or some where else
I need to run the automation by calling this TestNGCreator.main(null);
my another project is automation project in java TestNGCreator class resides in automation project.By calling this method i need to call above class and when i am doing it i am getting error.
Error :
May 17, 2019 4:20:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/webservices] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: org/testng/TestNG] with root cause
java.lang.ClassNotFoundException: org.testng.TestNG
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1955)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1798)
at testDrivers.TestNGCreator.runTestNGTest(TestNGCreator.java:44)
at testDrivers.TestNGCreator.main(TestNGCreator.java:165)
at com.xyz.webservices.Resource.executeScript(Resource.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:143)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:158)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:303)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:317)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
File : Resource.java
#POST
#Path("/execute")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response executeScript(String data) throws JSONException
{
ArrayList<Object> testCases = new ArrayList<Object>();
JSONArray jsonArray = new JSONArray();
JSONObject json = new JSONObject(data);
String configFile = (String) json.get("ConfigFiles");
String sheetName = (String) json.get("Sheetname");
jsonArray = (JSONArray) json.get("testCases");
for (int i = 0 ; i < jsonArray.length() ; i++)
{
testCases.add(jsonArray.get(i));
}
try {
TestNGCreator.main(null); //here i am calling that class main method
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Response response = null;
System.out.println(jsonArray);
System.out.println(testCases);
return response;
}
File : TestNGCreator.java
package testDrivers;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import projlib.Globals;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestNGCreator
{
public void runTestNGTest() throws IOException
{
System.out.println("Print this line in console");
//Create an instance on TestNG
TestNG myTestNG = new TestNG();
//Create an instance of XML Suite and assign a name for it.
XmlSuite mySuite = new XmlSuite();
mySuite.setName(Globals.SUITE_NAME);
//Create a list of XmlTests and add the Xmltest you created earlier to it.
List<XmlTest> myTests = new ArrayList<XmlTest>();
XSSFSheet excelWSheet = null;
XSSFWorkbook excelWBook = null;
XSSFCell cell = null;
XSSFRow row = null;
Double dblCellVal;
String strCellVal = null;
Boolean blnCellVal;
FileInputStream excelFile = new FileInputStream(Globals.CONFIG_FILE_DIR+"/CropRecordConfig.xlsx");
excelWBook = new XSSFWorkbook(excelFile);
excelWSheet = excelWBook.getSheet("CropAdd");
Iterator <Row> rowIterator = excelWSheet.iterator();
//Iterator <Cell> cellIterator = row.cellIterator();
String testName = null;
int colCount;
if (rowIterator.hasNext())
{
row = (XSSFRow) rowIterator.next();
colCount = row.getPhysicalNumberOfCells();
}
while (rowIterator.hasNext())
{
row = (XSSFRow) rowIterator.next();
Iterator <Cell> cellIterator = row.cellIterator();
int curCell = 0;
while (cellIterator.hasNext())
{
cell = (XSSFCell) cellIterator.next();
curCell++;
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
dblCellVal = cell.getNumericCellValue();
strCellVal = dblCellVal.toString();
break;
case Cell.CELL_TYPE_STRING:
strCellVal = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
blnCellVal = cell.getBooleanCellValue();
strCellVal = blnCellVal.toString();
break;
}
//If it is first cell then store the Test Name
if (cell.getColumnIndex()== 0)
{
testName = strCellVal;
}
if (curCell == 5) {
if (strCellVal.equals("1.0")) {
//Adding to suite
//Create an instance of XmlTest and assign a name for it.
XmlTest myTest = new XmlTest(mySuite);
myTest.setName(testName);
//Add any parameters that you want to set to the Test.
Map<String, String> testngParams = new HashMap<String,String> ();
testngParams.put("testId", testName);
myTest.setParameters(testngParams);
//Create a list which can contain the classes that you want to run.
List<XmlClass> myClasses = new ArrayList<XmlClass> ();
myClasses.add(new XmlClass("TestDriver"));
//Assign that to the XmlTest Object created earlier.
myTest.setXmlClasses(myClasses);
//Adding the test to test list created earlier
myTests.add(myTest);
break;
}
}
}
}
excelWBook.close();
//add the list of tests to your Suite.
mySuite.setTests(myTests);
//Add the suite to the list of suites.
List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
mySuites.add(mySuite);
//Set the list of Suites to the testNG object you created earlier.
myTestNG.setXmlSuites(mySuites);
File file = new File(Globals.TESTNG_FILE_NAME);
System.out.println("File is: " + file);
FileWriter writer = new FileWriter(file);
writer.write(mySuite.toXml());
writer.close();
//invoke run() - this will run your class.
//myTestNG.run();
}
public static void main(String args[]) throws IOException
{
TestNGCreator testDriver = new TestNGCreator();
testDriver.runTestNGTest();
}
}
File : POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>webservices</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservices</name>
<build>
<finalName>webservices</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<!-- uncomment this to get JSON support-->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Any suggestion will be appreciated thank you ^.^
I do not see the test-ng dependency in your pom file.
Please add the below and try.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>

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?

Getting error while reading xlsx file through apche poi

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

json-schema-validator NoClassDefFoundError: com/google/common/io/Closer

Solution: Add the guava dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
I'm trying to use the json-schema-validator over here: https://github.com/fge/json-schema-validator .
I'm using the following maven dependency:
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.5</version>
</dependency>
Here is a class I'm trying:
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
public class JsonSchemaTest {
public static void main(String[] args) throws IOException {
JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}");
}
}
And here is the error I'm receiving:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Closer
at com.github.fge.jackson.JsonNodeReader.fromReader(JsonNodeReader.java:120)
at com.github.fge.jackson.JsonLoader.fromReader(JsonLoader.java:179)
at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:192)
at com.cisco.webex.squared.flume.JsonSchemaTest.main(JsonSchemaTest.java:10)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.Closer
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 4 more
--- Edit:
If I change my maven version to 2.1.7, I can do JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}"); but I cannot create the factory here without the same java.lang.NoClassDefFoundError: com/google/common/io/Closer error:
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.report.ProcessingReport;
public final class JsonSchemaTest {
public static void main(final String[] args) throws IOException, ProcessingException {
//JsonNode data = JsonLoader.fromString("{\"a\":1}");
final JsonNode data = JsonLoader.fromString("{\"a\":1}");
final JsonNode fstabSchema = JsonLoader.fromString("{\"type\":\"object\", \"properties\":{\"a\":{\"type\":\"number\"}}}");
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(fstabSchema);
ProcessingReport report;
report = schema.validate(data);
System.out.println(report);
}
}
You have to import guava library in your project.
http://www.java2s.com/Code/Jar/g/Downloadguava150rc1jar.htm

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