Bimbz
Bimbz

Reputation: 453

How to read excel file on a Java-Web Start Application

I am developing a java-web-start application that reads excel files using java POI. My problem is whenever I run my code it returns an error.

Exception in thread "Thread-13" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/WorkbookFactory
    at digicare.tracking.serial.BulkUpload.UploadProgress$1read2.run(UploadProgress.java:89)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.WorkbookFactory
    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 com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

Here's my code:

try {
            InputStream inp = new FileInputStream(FilePath);
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        int maxrows = sheet.getPhysicalNumberOfRows();
        for (int r = 1; r < maxrows; r++) { 
            Thread.sleep(1000);
            lblCurrentRow.setText(String.valueOf(r));
            prbProgBar.setValue(r);                                       
                    txtOutPut.append(String.format("Done!!\n"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Upvotes: 1

Views: 1229

Answers (3)

Tourki
Tourki

Reputation: 1774

I encountered this before, actually there is not only one jar file to add, there is another one, named : poi-ooxml-x.jar (where x is the version) , it's this file that contains WorkbookFactory, not the poi-x.jar, you have to include both of them

----------------------------------------------------------------------------

EDIT I did some research and i think your classpath problem is related to your project being a java web start application :

Can I rely on Class-Path in the Manifest file?

Java Web Start does not support the Class-Path entry in the manifest file. The Class-Path attribute is entirely file-centric, where as Java Web Start and JNLP is Web-centric, i.e., based on URLs. Thus, the two models do not merge easily.

Instead of relying on the Class-Path entry, you can list multiple JAR files in the JNLP file, e.g.,:

<resources>
     <jar href="A.jar"/> 
     <jar href="B.jar"/>  
</resources>

here is the link where i found it : http://webstartfaq.com/#52

Upvotes: 1

Gagravarr
Gagravarr

Reputation: 48376

You are missing some of the POI jars. Specifically, you are certainly missing the poi-ooxml jar, but you may be missing some others too...

Apache POI provide a handy page listing components, their jars and their dependencies. As that page explains, if you want to use the common org.apache.poi.ss.usermodel classes (eg WorkbookFactory) you need the main POI jar, the POI-OOXML jar, and their respective dependencies.

If you add those two jars and their dependencies, and/or switch to maven and list your project as depending on poi plus poi-ooxml you'll have the classes you need

Upvotes: 0

poohdedoo
poohdedoo

Reputation: 1268

Looks like it cant find the WorkbookFactory. Assuming you have put Apache POI jar in the build path. Can you see if org.apache.poi.ss.usermodel.WorkbookFactory class is there in the jar file it self?

Upvotes: 0

Related Questions