Opening .xlsx file with Apache POI gives NoClassDe

2019-02-26 04:58发布

问题:

I'm having trouble getting the import of all the required .jar files working correctly. My end goal is simply to read in an excel spreadsheet (more specifically .xlsx files) to check whether the speed of Apache POI will work for future applications.

My question is why am I getting thrown the error...

java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException

...when I can see in the class InvalidFormatException at /org/apache/poi/openxml4j/exceptions/ in the 'file poi-ooxml-3.9-20121203.jar'?

I haven't done programming in Java in a while and I'm hoping for my own sanity that I've made a stupid mistake. My code seems to be compiling correctly but I have the above error message when I try run the program. I have done some research into my issue but haven't been able to find someone in my situation. They usually just forget/don't have a required .jar file. Maybe I'm in the same boat but hopefully you'll be able to tell me.

I'll give a breakdown of my logic. Here is the code in TestExcel.java

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

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.ss.usermodel.WorkbookFactory; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

public class TestExcel 
{
    public static void main(String[] args) 
    { 
        InputStream inp; 
        try 
        { 
            inp = new FileInputStream("cost.xlsx"); 

            Workbook wb; 
            try { 
                wb = WorkbookFactory.create(inp); 
            } 
            catch (FileNotFoundException e) { 
                e.printStackTrace(); 
            } 
        } 
        catch (InvalidFormatException e) { 
            e.printStackTrace(); 
        } 
        catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
}

Here is the file 'c_test' used to compile and run the java code:

javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java TestExcel

Here is the directory listing of the classpath above:

robsco@roblaptop:~/Webdev/java/poi$ ls -al
total 26616
drwxrwxr-x 2 robsco robsco     4096 Aug 18 18:13 .
drwxrwxr-x 6 robsco robsco     4096 Aug 18 18:12 ..
-rwxr-xr-- 1 robsco robsco    52915 Sep 18  2009 commons-logging-1.1.jar
-rwxr-xr-- 1 robsco robsco   313898 Apr  5  2009 dom4j-1.6.1.jar
-rwxr-xr-- 1 robsco robsco   121070 Apr  5  2009 junit-3.8.1.jar
-rwxr-xr-- 1 robsco robsco   358180 Apr  5  2009 log4j-1.2.13.jar
-rwxrw-r-- 1 robsco robsco 14841622 Aug 18 13:15 ooxml-schemas-1.1.jar
-rwxr-xr-- 1 robsco robsco  1869113 Nov 26  2012 poi-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco    30446 Nov 26  2012 poi-excelant-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco   936648 Nov 26  2012 poi-ooxml-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco  4802621 Nov 26  2012 poi-ooxml-schemas-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco  1226511 Nov 26  2012 poi-scratchpad-3.9-20121203.jar
-rwxr-xr-- 1 robsco robsco  2666695 Apr  5  2009 xmlbeans-2.3.0.jar

and the output of executing 'c_test':

robsco@roblaptop:~/Webdev/java$ ./c_test 
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/openxml4j/exceptions/InvalidFormatException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
    at java.lang.Class.getMethod0(Class.java:2764)
    at java.lang.Class.getMethod(Class.java:1653)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.exceptions.InvalidFormatException
    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:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

I am using:
OS: Lubuntu 12.10 x86

java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)

robsco@roblaptop:~/Webdev/java/poi$ javac -version
javac 1.7.0_25

回答1:

The problem is with how you're launching your program. I see you currently have a shell script of:

javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java TestExcel

That compiles with all the jars, but doesn't run with them, so your program won't have its dependencies available. (Unlike with some c programs, compiling with Java won't inline the depdendencies, nor will it add local links to the files. There are tools that will do that for you if you really need it though)

If you change your shell script to be:

javac -classpath "/home/robsco/Webdev/java/poi/*" TestExcel.java
java -classpath ".:/home/robsco/Webdev/java/poi/*" TestExcel

It should work fine. Note the addition of the current directory in the classpath in order for 'java' to find your class (in this case TestExcel).

Alternately, you could do something a little icky with environment variables, using the CLASSPATH envvar that Java checks, eg

export CLASSPATH=.:`ls -1d --color=no /home/robsco/Webdev/java/poi/* | xargs echo | sed 's/ /\:/g'`
javac TestExcel.java
java TestExcel