javaapache-poicommand-line-interface

Unable to run jar from cli, poi-ooxml ...xssf.usermodel.XSSFCellSytle ignored


I have a simple java program which creates a pre-formatted excel file from a csv, what I would like to get out of the IDE to run as jar file on a windows host. Within Netbeans it's working since years, but when I hit clean and build to generate the jar then test it from command line with java command then I get the following error: "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/CellStyle"

This is strange to me, because I do not use SS.usermodel.CellStyle, but use XSSF.usermodel.XSSFCellStyle interface. Why does it complain for SS? Poi and poi-ooxml dependencies both included with the same version 5.0.0 cause I use classes I only found in simple poi.

I'm an enthusiast so it is possible that I forgot something basic, but I really don't know what. Can you help me? :) Any further details needed?

Further details as per request:

java -jar /Users/csomabalint/Java/formatpenaltyfile/target/formatpenaltyfile-1.0-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/CellStyle 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:650) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632) Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.CellStyle at java.net.URLClassLoader.findClass(URLClassLoader.java:387) at java.lang.ClassLoader.loadClass(ClassLoader.java:419) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:365) at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ... 7 more

Running in Netbeans:

cd /Users/csomabalint/Java/formatpenaltyfile; JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home "/Applications/NetBeans/Apache NetBeans 12.2.app/Contents/Resources/NetBeans/netbeans/java/maven/bin/mvn" "-Dexec.args=-classpath %classpath com.xxx.formatpenaltyfile.FormatterKlass" -Dexec.executable=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/bin/java org.codehaus.mojo:exec-maven-plugin:3.0.0:exec Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts. Scanning for projects...

---------------------< com.xxx:formatpenaltyfile >---------------------- Building formatpenaltyfileNew 1.0-SNAPSHOT --------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:3.0.0:exec (default-cli) @ formatpenaltyfile ---

BUILD SUCCESS

Total time: 13.118 s Finished at: 2024-09-23T19:25:16+02:00

The snapshot is created automatically, but I can create that by right click on the project within Netbeans IDE and click "Clean" and after "Build with dependencies"

Building log:

cd /Users/csomabalint/Java/formatpenaltyfile; JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home "/Applications/NetBeans/Apache NetBeans 12.2.app/Contents/Resources/NetBeans/netbeans/java/maven/bin/mvn" install

--- maven-compiler-plugin:3.1:compile (default-compile) @ formatpenaltyfile --- Changes detected - recompiling the module! Compiling 2 source files to /Users/csomabalint/Java/formatpenaltyfile/target/classes

--- maven-resources-plugin:2.6:testResources (default-testResources) @ formatpenaltyfile --- Using 'UTF-8' encoding to copy filtered resources. skip non existing resourceDirectory /Users/csomabalint/Java/formatpenaltyfile/src/test/resources

--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ formatpenaltyfile --- Nothing to compile - all classes are up to date

--- maven-surefire-plugin:2.12.4:test (default-test) @ formatpenaltyfile --- No tests to run.

--- maven-jar-plugin:3.2.0:jar (default-jar) @ formatpenaltyfile --- Building jar: /Users/csomabalint/Java/formatpenaltyfile/target/formatpenaltyfile-1.0-SNAPSHOT.jar

--- maven-install-plugin:2.4:install (default-install) @ formatpenaltyfile --- Installing /Users/csomabalint/Java/formatpenaltyfile/target/formatpenaltyfile-1.0-SNAPSHOT.jar to /Users/csomabalint/.m2/repository/com/xxx/formatpenaltyfile/1.0-SNAPSHOT/formatpenaltyfile-1.0-SNAPSHOT.jar Installing /Users/csomabalint/Java/formatpenaltyfile/pom.xml to /Users/csomabalint/.m2/repository/com/xxx/formatpenaltyfile/1.0-SNAPSHOT/formatpenaltyfile-1.0-SNAPSHOT.pom

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx</groupId>
    <artifactId>formatpenaltyfile</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
    <exclusions>
     <exclusion>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
     </exclusion>
    </exclusions>
</dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
               <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.xxx.formatpenaltyfile.FormatterKlass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build> 
    <name>formatpenaltyfileNew</name> 
</project>


Solution

  • Although trashgod's comment pointing to https://stackoverflow.com/a/17654214/230513 might solve the problem with JDK 8 and Netbeans, the situation changed and I found an alternate solution by using Visual Studio Code, which automatically included the necessary plugin dependency into the Pom file and I only had to export my project and write the runnable jar.