0

when i try to launch JAR file im getting this exception, I've changed poi version about 4 times to try different versions but still JAR doesn't want to launch. It's a test program because in other I couldn't launch JAR and I don't want to break anything in there.

I'm using JDK 17 and also tested it with JDK 13.

App: package com;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {

    public static void main(String[] args) {

        int x = 7;
        System.out.println(x);

        XSSFWorkbook workbook = new XSSFWorkbook();

    }

}

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.JAR</groupId>
    <artifactId>probaJar</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>



    </dependencies>

    <build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>
                                    com.Main
                                </mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <!-- Configures the content of the created manifest -->
                <manifest>
                    <!-- Adds the classpath to the created manifest -->
                    <addClasspath>true</addClasspath>
                    <!-- Specifies that all dependencies of our application are found -->
                    <!-- Configures the main class of the application -->
                    <mainClass>com.Main</mainClass>
                </manifest>
            </archive>
        </configuration>
        </plugin>

    </plugins>


    </build>

</project>
  • 2
    Why you do not use last version of Apache POI? I tried your code and it woks. – JHDev Jan 24 '22 at 11:16
  • Just downloaded the latest version and still im getting this message: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook at com.Main.main(Main.java:14) Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 1 more – Patryk Łasek Jan 24 '22 at 12:03
  • 1
    If you want to try gradle - this example works - https://github.com/pjfanning/poi-gradle-example – PJ Fanning Jan 24 '22 at 14:04
  • How are you "launching the JAR file"? What IDE are you using? At runtime, you need to give the running process a classpath that contains the JAR file. – Garet Jax Feb 02 '22 at 13:25

1 Answers1

0

Reason why error like this occurs on stack overfrow

Maybe you're trying to launch wrong jar file. There are two jar files created after compilation and packaging (full names omitted):

  1. *.jar
  2. *-jar-with-dependencies.jar

You must launch second one, because first one does not contain needed dependency classes.

  • When I launch the second one im getting this message Exception in thread "main" java.lang.NoSuchMethodError: 'org.apache.xmlbeans.XmlOptions org.apache.xmlbeans.XmlOptions.setUseDefaultNamespace(boolean)' at org.apache.poi.xssf.model.SharedStringsTable.(SharedStringsTable.java:96) at org.apache.poi.ooxml.POIXMLFactory.newDocumentPart(POIXMLFactory.java:94) at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:591) at org.apache.poi.ooxml.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:500) at – Patryk Łasek Jan 24 '22 at 12:08
  • org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:464) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:255) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:249) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:237) at com.Main.main(Main.java:14) – Patryk Łasek Jan 24 '22 at 12:08
  • 2
    @PatrykŁasek Have you recompiled and repackaged whole project after updating version? Did you updated all dependencies to newest versions or only one of them? Can you update `pom.xml` in your question ? – Artem Evdokimov Jan 24 '22 at 12:23
  • I' ve recompiled project and repacked it org.apache.poi poi 5.0.0 compile org.apache.poi poi-ooxml 5.0.0 compile – Patryk Łasek Jan 24 '22 at 12:40
  • org.apache.poi poi-ooxml-schemas 4.1.2 compile – Patryk Łasek Jan 24 '22 at 12:41
  • 1
    There are two ways to fix this. First one is to set all dependencies versions to 4.1.2, second one is to remove poi-ooxml-schemas from dependencies – Artem Evdokimov Jan 24 '22 at 12:47