[英]Apache POI in executable jar
我的程序成功運行了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-ooxml
和xmlbeans
。 (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被忽略。
因此,在下載了我在原始帖子中列出的一組罐子之后,我確實嘗試了以下變化。
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系統上很好地運行該程序。 因此,很顯然,LINUX系統上POI需要的東西不在Windows計算機上。
(請注意,ae在LINUX上運行良好。它無法在PC ==上運行,因此我在LINUX中設置的環境需要某些東西,但不知道那是什么。但是,我也再次檢查了將ae放入可執行jar並在同一LINUX環境上運行它。它會產生相同的問題。)
當然,我也嘗試從poi.apache.org
下載新的4.0, poi.apache.org
六個jar文件包含在zip文件poi-bin-4.0.0...zip
)
您有2個職位
將這些jar包含在您的jar中,並在META-INF / MANIFEST.MF中設置類路徑
Manifest-Version: 1.0
Class-Path: file1.jar file2.jar file3.jar
或在調用Java時將這些jar添加到classpath
java -classpath \path_to_jars\*.jar your.package.YourMainClass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.