gpt4 book ai didi

java - 可执行 jar 中的 Apache POI

转载 作者:行者123 更新时间:2023-12-02 01:53:37 25 4
gpt4 key购买 nike

我的程序成功运行 Apache POI 功能来读取Excel 文件。当我使用 java 类名运行它时,它工作正常。当我打包为可执行 jar 时,它不起作用。这里是我不断收到错误消息:

java -jar some.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/Row
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.Row
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

当我正常运行时它工作正常,例如。克。

java a.e

通过阅读堆栈溢出,我知道我已经poi-ooxmlxmlbeans。 (20688851, 15831591)

这是我的可执行 jar 根目录中的 jar 文件。

5924600 Wed Sep 12 13:02:26 CDT 2018 poi-ooxml-schemas-3.17.jar
1479023 Wed Sep 12 13:02:26 CDT 2018 poi-ooxml-3.17.jar
1390360 Tue Oct 02 13:04:14 CDT 2018 poi-scratchpad-3.17.jar
313359 Tue Oct 02 13:17:34 CDT 2018 dom4j.jar
25863 Tue Oct 02 13:19:46 CDT 2018 stax-api.jar
1950905 Wed Sep 12 13:02:26 CDT 2018 poi-3.10-FINAL-20140208.jar
2730866 Tue Oct 02 12:43:34 CDT 2018 xmlbeans-2.6.0.jar

这是我演示问题的最小测试用例。

package a;
import java.io.*;
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;

public class e {

static PrintWriter F;
static FileInputStream ConfigurationFile;
static String iSheet = "TAB2";

private static final String strResourceName = "11in35tr.xls";


static
{
java.io.InputStream streamData = null;
try
{
streamData=a.e.class.getResourceAsStream(strResourceName);
}
catch(NullPointerException e)
{
System.out.println("Unable to load the resource stream for '" + strResourceName + "'");

assert false;
}
System.out.println (" stream Data |" + streamData+"|");
org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = null;
try
{
workbook = new org.apache.poi.hssf.usermodel.HSSFWorkbook(streamData);
}
catch(java.io.IOException e)
{
System.out.println("Unable to create a HSSFWorkbook for the stream. Is it really an Excel file?");

assert false;
}
String Sheet;
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.getSheet(iSheet);
if(sheet == null) {
System.out.println("While we were able to open the resource as an Excel file, it doesn't appear to contain sheet " + sheet.toString() + " as specified in the config file");
}
for(int iCurRow = 0;iCurRow<3;iCurRow++)
{
org.apache.poi.ss.usermodel.Row rowCur = sheet.getRow(iCurRow);
if(rowCur == null)
{
System.out.println("We're supposed to get a row title from row " +
Integer.toString(iCurRow) + " in sheet " + iSheet + ", but that row doesn't exist");

break;
}
for (int iCol=0;iCol<3;iCol++) {
org.apache.poi.ss.usermodel.Cell
cellWithTitle = rowCur.getCell(iCol);
if(cellWithTitle == null)
{
System.out.println("We're supposed to get a row title from cell " + Integer.toString(iCol) + " in row " + Integer.toString(iCurRow) + " in sheet " + iSheet + ", but that column doesn't appear to exist in the specified sheet.");

break;
}
String strNewRowTitle = cellWithTitle.toString();

System.out.println("Created row: '" + strNewRowTitle + "'");
}
} // for


} // end of static

static void Init () throws java.io.IOException {
ConfigurationFile = new FileInputStream ("config.in");
F = new PrintWriter (new FileOutputStream ("config.out"));
}

public static void main (String args[]) throws IOException {
Init();

F.close();
}
}
<小时/>

不幸的是,这些都不起作用。首先,我更新了manifest.txt 文件。 (如图所示)

Main-Class: a.c
Class-path: poi-ooxml-schemas-3.17.jar poi-ooxml-3.17.jar poi-scratchpad-3.17.jar dom4j.jar stax-api.jar poi-3.10-FINAL-2014028.jar xmlbeans-2.6.0.jar

然后,我尝试了该命令

java -classpath *.jar -jar some.jar 
java -classpath "poi-ooxml-schemas-3.17.jar:poi-ooxml-3.17.jar:poi-scatchpad-3.17.jar:dom4.jar:stax-api.jar:poi-3.10-FINAL-2014028.jar:xmlbeans-2.6.0.jar" -jar some.jar

我的理解是,当我们使用可执行 jar 时,-classpath 会被忽略。

因此,在下载了我在原始帖子中列出的一组 jar 后,我确实尝试了以下变体。

set CLASSPATH=".;poi-ooxml-schemas-3.17.jar;poi-ooxml-3.17.jar;poi-scatchpad-3.17.jar;dom4.jar;stax-api.jar;poi-3.10-FINAL-2014028.jar;xmlbeans-2.6.0.jar" 
echo %CLASSPATH%
java a.e

他们以同样的方式失败。不过,我可以在Linux系统上很好地运行该程序。因此,显然,有一些东西在 POI 需要的 LINUX 系统上,而不是在 Windows 计算机上。

(注意 a.e 在 LINUX 上运行良好。它不能在 PC 上运行==,因此我在 LINUX 中设置的环境需要一些东西,但不知道那是什么。不过,我还再次检查了将 a.e 放入可执行 jar 中并在相同的 LINUX 环境中运行它。它会产生同样的问题。)

当然,我也尝试从 poi.apache.org 下载新的 4.0,并将六个 jar 文件包含在 zip 文件 poi-bin-4.0.0... zip )

最佳答案

你有两种可能性

将这些 jar 包含在您的 jar 中,并在 META-INF/MANIFEST.MF 中设置类路径

Manifest-Version: 1.0
Class-Path: file1.jar file2.jar file3.jar

或者在调用 java 时将这些 jar 添加到类路径

java -classpath \path_to_jars\*.jar your.package.YourMainClass

关于java - 可执行 jar 中的 Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52614410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com