I am creating a program with NetBeans IDE 17 that can write to a .xlsx file when I press a button in on a JFrame. When I run the code in NetBeans, it works as intended but, when I build the program and try to run the .jar executable through the console, I get java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook and I am not sure why. I have added the dependencies with the "Add dependencies" button in NetBeans. This is what the pom.xml looks like:

<?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.mycompany</groupId>
    <artifactId>workbookTesting</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <exec.mainClass>com.mycompany.workbooktesting.NewJFrame</exec.mainClass>
    </properties>
    <build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.1.0</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>com.mycompany.workbooktesting.NewJFrame</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
</project>
1

There are 1 best solutions below

0
On

Adding the dependency jars in your IDE does not automatically add them to the jar file you are trying to execute. That file contains your own code. You need to specify what to put on the classpath when running from a console either via

java -cp <classpath> -jar <jarfile>

Or better create a startup script, that does this for you. Like for example the script that starts maven from your console (mvnw/mwnw.bat).

I do not know your IDE, maybe it can produce such a file for you based on the cp you have defined in your IDE.