TransWikia.com

java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook

Stack Overflow en español Asked by Nassif 113 on November 7, 2021

He creado una app en NetBeans, y al compilar el proyecto la app funciona correctamente. Al exportar un jar con Build (construir) el abrir el archivo mediante CMD java -jar app.jar me lanza la siguiente exception:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
        at Frames.LCDAMain.searchActionPerformed(LCDAMain.java:625)
        at Frames.LCDAMain.access$1400(LCDAMain.java:21)
        at Frames.LCDAMain$9.actionPerformed(LCDAMain.java:391)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 40 more

En mi app se lee un acrchivo excell, empleando la librería de Apache Poi. Para la distribución de las dependencias y librerías uso Maven, y en el POM.xml del proyecto tengo (las dependencias están aisladas al final) :


<?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>LCDAmanger</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        
<plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>Frames.LCDAMain</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
        <showDeprecation>true</showDeprecation>
        </configuration>
        </plugin>
       </plugins>
    </build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
    <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>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <configuration>
                    <configLocation>config/sun_checks.xml</configLocation>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

    <dependencies>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi-ooxml</artifactId>
     <version>4.1.2</version>
     <type>jar</type>
</dependency>

<dependency>
     <groupId>commons-codec</groupId>
     <artifactId>commons-codec</artifactId>
     <version>1.13</version>
</dependency>

    </dependencies>

</project>

2 Answers

La forma más sencilla, si el uno está desarrollando su proyecto usando Maven, es que utilice el Plug-In de Maven Shade.

Lo que está pasando aquí es que el Jar resultante no tiene recogido también las dependencias/librerías. Por ello es necesario que se especifique en el POM.xml de Maven añadiendo el siguiente plug-in:


<plugins>
<!-- TUS PLUG-INS -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version> <!-- ESCOGE LA ÚLTIMA VERSIÓN -->
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
            <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>"MiPaquete.NombreDeMiClaseMain"</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
    </plugin>

<!-- TUS PLUG-INS -->

</plugins>

Lo elegante de esto, es que el próximo paso para construir el Jar es tan simple como hacer click en Build (o contruir) con el símbolo de martillo en NetBeans y listo.

Answered by Nassif 113 on November 7, 2021

Lo que ocurre es que el jar que estás generando no contiene ninguna dependencia más allá de las bibliotecas estándar provistas por el JRE. Una manera de solucionarlo es crear un "fat jar" que contiene todas tus dependencias en un solo jar, y es fácil de copiar o mover a otros servidores o PCs En Netbeans no lo puedes hacer directamente, pero aquí tiene una explicación de cómo hacerlo: https://www.nysimtech.com/2019/02/05/como-crear-un-fat-jar-en-netbeans/ Otra opción es utilizar el plugin maven-assembly y crear ej JAR con Maven.

Answered by Alex on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP