admin管理员组文章数量:1122846
I am running AlmaLinux Linux 8.10 (Cerulean Leopard)
Running the command java -version
gives:
openjdk version "1.8.0_432"
OpenJDK Runtime Environment (build 1.8.0_432-b06)
OpenJDK 64-Bit Server VM (build 25.432-b06, mixed mode)
I have downloaded the Apache POI .jar files (extracted from poi-bin-5.2.3-20220909.zip from /).
I have then taken the example Create Blank Workbook
from .htm that creates a blank Excel file:
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook {
public static void main(String[] args)throws Exception {
//Create Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create file system using specific name
FileOutputStream out = new FileOutputStream(new File("createworkbook.xlsx"));
//write operation workbook using file out object
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
}
}
Next I have "compiled" the program using the command
> javac -classpath /home/public/INFO/java_scripts/userlib/poi-5.2.3.jar:/home/public/INFO/java_scripts/userlib/poi-ooxml-5.2.3.jar CreateWorkBook.java
This step generates no errors. Next I run
> java CreateWorkBook
Which generates the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
at CreateWorkBook.main(CreateWorkBook.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
at java.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 1 more
From the error message the line that is failing is XSSFWorkbook workbook = new XSSFWorkbook();
So, the question is, why?
I am running AlmaLinux Linux 8.10 (Cerulean Leopard)
Running the command java -version
gives:
openjdk version "1.8.0_432"
OpenJDK Runtime Environment (build 1.8.0_432-b06)
OpenJDK 64-Bit Server VM (build 25.432-b06, mixed mode)
I have downloaded the Apache POI .jar files (extracted from poi-bin-5.2.3-20220909.zip from https://archive.apache.org/dist/poi/release/bin/).
I have then taken the example Create Blank Workbook
from https://www.tutorialspoint.com/apache_poi/apache_poi_workbooks.htm that creates a blank Excel file:
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook {
public static void main(String[] args)throws Exception {
//Create Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create file system using specific name
FileOutputStream out = new FileOutputStream(new File("createworkbook.xlsx"));
//write operation workbook using file out object
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
}
}
Next I have "compiled" the program using the command
> javac -classpath /home/public/INFO/java_scripts/userlib/poi-5.2.3.jar:/home/public/INFO/java_scripts/userlib/poi-ooxml-5.2.3.jar CreateWorkBook.java
This step generates no errors. Next I run
> java CreateWorkBook
Which generates the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
at CreateWorkBook.main(CreateWorkBook.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 1 more
From the error message the line that is failing is XSSFWorkbook workbook = new XSSFWorkbook();
So, the question is, why?
1 Answer
Reset to default 2The file poi-bin-5.2.3-20220909.zip
includes all the libraries and transitive dependencies that Apache Poi uses. All these libraries need to be included in the classpath at runtime.
The TutorialsPoint website lists on this page (Step 3) all the libraries you need in your classpath. However, it's easier if you use the *
syntax to include all of the .jar
files in a specified directory, so to run your Java program all you need is the following command line:
java -cp 'poi-bin-5.2.3/*:poi-bin-5.2.3/lib/*:poi-bin-5.2.3/ooxml-lib/*:.' CreateWorkBook
Note the -cp
option value is quoted in single quotes because we don't want the shell to do any glob expansion.
本文标签: Java Apache POI to create Excel workbook not working (Linux)Stack Overflow
版权声明:本文标题:Java Apache POI to create Excel workbook not working (Linux) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312155a1934997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
java -classpath /home/public/INFO/java_scripts/userlib/poi-5.2.3.jar:/home/public/INFO/java_scripts/userlib/poi-ooxml-5.2.3.jar:. CreateWorkBook
. – k314159 Commented Nov 21, 2024 at 10:48NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
. This difference is an important detail. The new error is because Apache Poi depends on some other Apache libraries. – k314159 Commented Nov 21, 2024 at 12:03