使用 Apache POI 打开 .xlsx 文件会出现 NoClassDefFoundError InvalidFormatException [英] Opening .xlsx file with Apache POI gives NoClassDefFoundError InvalidFormatException

查看:67
本文介绍了使用 Apache POI 打开 .xlsx 文件会出现 NoClassDefFoundError InvalidFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确导入所有必需的 .jar 文件.我的最终目标只是读取 excel 电子表格(更具体地说是 .xlsx 文件)以检查 Apache POI 的速度是否适用于未来的应用程序.

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

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

...当我可以在文件 poi-ooxml-3.9-20121203.jar"中的/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'?

我有一段时间没有用 Java 进行编程了,我希望我自己犯了一个愚蠢的错误.我的代码似乎编译正确,但是当我尝试运行该程序时出现上述错误消息.我已经对我的问题进行了一些研究,但无法找到符合我情况的人.他们通常只是忘记/没有必需的 .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.

我会分解我的逻辑.这是TestExcel.java中的代码

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

这是用于编译和运行 java 代码的文件c_test":

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

和执行'c_test'的输出:

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

我正在使用:
操作系统:Lubuntu 12.10 x86

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

推荐答案

问题在于您启动程序的方式.我看到你目前有一个 shell 脚本:

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

这与所有 jars 一起编译,但不与它们一起运行,因此您的程序将没有可用的依赖项.(与某些 c 程序不同,使用 Java 编译不会内联依赖关系,也不会向文件添加本地链接.如果您确实需要,有一些工具可以为您执行此操作)

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)

如果您将 shell 脚本更改为:

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

它应该可以正常工作.请注意在类路径中添加了当前目录,以便java"找到您的类(在本例中为 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).

或者,您可以使用 Java 检查的 CLASSPATH 环境变量对环境变量做一些有点棘手的事情,例如

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

这篇关于使用 Apache POI 打开 .xlsx 文件会出现 NoClassDefFoundError InvalidFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆