您好,我在运行jar时遇到以下异常。有人可以帮忙吗?

U2007750s-MacBook-Pro:target u2007750$ java -jar ExcelCSVConverter-0.0.1-SNAPSHOT.jar
    Error: A JNI error has occurred, please check your installation and try again
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
        at java.lang.Class.getMethod0(Class.java:3018)
        at java.lang.Class.getMethod(Class.java:1784)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more


更新:

到目前为止,我在pom.xml中仅具有poi-ooxml依赖项:

     <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>


该程序在Eclipse中正确执行。仅在命令行中发生错误。

现在在你们的推荐之后,我还添加了第二个依赖项poi

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>


但仍然没有成功。现在,该类发生错误:

U2007750s-MacBook-Pro:target u2007750$ java -jar ExcelCSVConverter-0.0.1-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/impl/values/XmlValueOutOfRangeException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more


方法是:

private static String getCellData(XSSFCell myCell) {
        String cellData = "";
        if (myCell == null) {
            cellData += CSV_SEPERATOR_CHAR;
        } else {
            try {
                switch (myCell.getCellTypeEnum()) {
                case STRING:
                case BOOLEAN:
                    cellData += myCell.getRichStringCellValue() + CSV_SEPERATOR_CHAR;
                    break;
                case NUMERIC:
                    cellData += getNumericValue(myCell);
                    break;
                case FORMULA:
                    cellData += getFormulaValue(myCell);
                default:
                    cellData += CSV_SEPERATOR_CHAR;
                }
            } catch (XmlValueOutOfRangeException e) {
                int a =2;
                //System.out.println("XmlValueOutOfRangeException for: "+ myCell.getRawValue());
                String rawValue = myCell.getRawValue();
                while(rawValue.startsWith("0")) {
                    rawValue = rawValue.substring(1);
                }
                cellData += rawValue+ CSV_SEPERATOR_CHAR;
            }
        }
        return cellData;
    }

最佳答案

您需要下载以下JAR文件,并将它们添加到您的构建路径中。

Apache POI
Apache POI-OOXML

如果您使用的是Maven:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>3.0.1</version>
</dependency>



如果您使用的是Graddle:

// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.17'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'

09-30 15:03