Error in Import Class defined in 2 .jar files with the same directory structure
I am trying to use Apache POI to process some excel document.
I am trying to just come up with a simple test to make sure I set up the package correctly, by compiling and running the test code found on the quick guide page: Busy Developers' Guide to HSSF and XSSF Features
Code:
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.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);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
I need to make imports. According to the documentation (http://poi.apache.org/apidocs/index.html) and also I went into apache poi's src / jar directory,
I have to import:
import org.apache.poi.ss.usermodel.*;
The problem is : WorkbookFactory is located in the .jar file poi-ooxml-3.8-20120326.jar,
and other major classes are located in poi-3.8-20120326.jar
both jar files have exactly the same directory structure.
I put both .jar files in classpath, and with my import statement, I get the following error:
testRead.java:3: cannot find symbol
symbol : class WorkbookFactory
location: package org.apache.poi.ss.usermodel
import org.apache.poi.ss.usermodel.WorkbookFactory;
^
testRead.java:14: cannot find symbol
symbol : variable WorkbookFactory
location: class testRead
Workbook wb = WorkbookFactory.create(inp);
^
2 errors
What could I do to get the compilation?
Re: Error in Import Class defined in 2 .jar files with the same directory structure
I did my googling, but I can't seem to find how to solve that. Most source addresses though, that WorkbookFactory is located in the different jar.
For e.g. Read and write Excel file using Apache POI | OpenWritings.net
Code:
/**
* Example showing how to read and write Excel file(i.e *.xls or *.xlsx).
* JAR files needed:
* poi-3.6-20091214.jar
* poi-ooxml-3.6-20091214.jar
* If you only need to handle Excel 2007 OOXML (.xlsx) file format, then you can use XSSF* classes.
* If you only need to handle Excel '97(-2007) file format, then you can use HSSF* classes.
* @author Xuan Ngo
*/
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory; // This is included in poi-ooxml-3.6-20091214.jar
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class PoiExample
{
public static void main(String[] args)
{
try
{
// Read from original Excel file.
Workbook workbook = WorkbookFactory.create(new FileInputStream("my_original_excel.xls") );
// Get the first sheet.
Sheet sheet = workbook.getSheetAt(0);
// Set value of the first cell.
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
cell.setCellValue("Xuan");
// Write newly modified workbook to a file.
FileOutputStream fileOut = new FileOutputStream("new_workbook.xls");
workbook.write(fileOut);
fileOut.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch(InvalidFormatException e)
{
System.out.println(e);
}
}
}
Re: Error in Import Class defined in 2 .jar files with the same directory structure
Quote:
Originally Posted by
JimmyD
I am trying to use Apache POI
Moved from New to Java
db
Re: Error in Import Class defined in 2 .jar files with the same directory structure
After more searching, I got this post: POI - User - java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
That kinda addresses some problem. Right now, when I compile, I include all the following jars in my -cp:
poi-3.8-20120326.jar;
poi-ooxml-3.8-20120326.jar;
ooxml-schemas-1.1.jar;
poi-scratchpad-3.8-20120326.jar;
poi-excelant-3.8-20120326.jar;
xmlbeans-2.5.0.jar;
poi-ooxml-schemas-3.8-20120326.jar
I include my code:
Code:
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory; // This is included in poi-ooxml-3.6-20091214.jar
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class testRead {
public static void main(String[] args) throws IOException
{
try
{
//InputStream inp = new FileInputStream("workbook.xls");
InputStream inp = new FileInputStream("workbook.xlsx");
//Workbook wb = WorkbookFactory.create(inp);
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);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
catch (Exception e)
{
System.out.println(e);
//e.printStackTrace();
}
/*
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch(InvalidFormatException e)
{
System.out.println(e);
e.printStackTrace();
}
*/
}
}
Under this setting, this code compiles. However, When I run it, it says:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/WorkbookFactory
at testRead.main(testRead.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.WorkbookFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
... 1 more
Re: Error in Import Class defined in 2 .jar files with the same directory structure
I Fixed my problem. Ty for everyone's attention!
Re: Error in Import Class defined in 2 .jar files with the same directory structure
Quote:
Originally Posted by
JimmyD
I Fixed my problem. Ty for everyone's attention!
HI,
I just want to know how you solved this problem. Iam also facing same problem.
Regards
manju