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?

Share Improve this question edited Nov 21, 2024 at 9:34 Chazg76 asked Nov 21, 2024 at 9:20 Chazg76Chazg76 6476 silver badges13 bronze badges 7
  • 2 You didn't add the jars from the compilation to the java command. – aled Commented Nov 21, 2024 at 10:07
  • 1 This question is similar to: How to run a java class with a jar in the classpath?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – seenukarthi Commented Nov 21, 2024 at 10:11
  • This is a duplicate. To clarify, you need 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:48
  • Thanks for the info. Followed the instructions on the suggested page but nothing works! This should be a simple example so giving up as already wasted too much time on this! – Chazg76 Commented Nov 21, 2024 at 10:50
  • 2 I've just tried it and yes, you do still get an error, but not exactly the same error as before. It's a different error message: NoClassDefFoundError: 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
 |  Show 2 more comments

1 Answer 1

Reset to default 2

The 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