Results 1 to 6 of 6
  1. #1
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default 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

    Java 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?

  2. #2
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default 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

    Java 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);
        }
     
      }
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: Error in Import Class defined in 2 .jar files with the same directory structure

    Quote Originally Posted by JimmyD View Post
    I am trying to use Apache POI
    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default 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:

    Java 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
    Last edited by JimmyD; 10-01-2012 at 10:21 PM.

  5. #5
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: Error in Import Class defined in 2 .jar files with the same directory structure

    I Fixed my problem. Ty for everyone's attention!

  6. #6
    manju_102 is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Re: Error in Import Class defined in 2 .jar files with the same directory structure

    Quote Originally Posted by JimmyD View Post
    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

Similar Threads

  1. Import my own class files into eclipse
    By teaicky in forum New To Java
    Replies: 3
    Last Post: 02-07-2011, 09:45 PM
  2. Zip Directory Structure
    By freestatedon in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 06:11 AM
  3. Directory Structure
    By rummy in forum New To Java
    Replies: 1
    Last Post: 01-21-2010, 12:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •