Maven executable Jar throws error on start [duplicate]
Asked Answered
T

1

6

First of all: I am new to maven. I made my first maven Application and successfully tested it within the IDE. The build was always successfull and everything worked like a charm.

Now I want to export the project as an executable jar with the dependencies built in, but I am not quite sure why it is not working.

I added the following to my pom file, as that was what I found on various answers to a similar question

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.pwc.scfa.pensareautomatio3.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
</build>

I understand that this specifies the main class for the JVM to start, as the IDE does not set this automatically.

I located the jar in the targets directory, copied it to another directory and tried to execute it.

Sadly the following errors are thrown:

enter image description here

enter image description here

Can you please give me a hint, where I might have gone wrong? That would be great. (I am using NetBeans, if that is of any help.)

Here is my StackTrace:

C:\Users\scfa\Desktop>java -jar PensareAutomatio-1.1.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/openxm
l4j/exceptions/InvalidFormatException
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.openxml4j.exceptions
.InvalidFormatException
        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)
        ... 7 more

Thanks :)

Trehalose answered 9/2, 2017 at 6:24 Comment(1)
can you open the jar file with an archiving tool (like 7zip or winrar)? If so, what could you post the contents of it? also, try to run it from command line to see the stack trace (in windows use cmd, navigate to the folder containing the jar and type "java -jar myjar.jar"Industrialism
I
5

If I'm correct, maven-jar-plugin creates a jar with all the compiled .class files, but without the dependencies.

I'd recommend using maven-assembly-plugin and binding it to the package execution phase, that way it would be built when running mvn install

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.pwc.scfa.pensareautomatio3.Main</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

See this answer for more information.

Industrialism answered 9/2, 2017 at 7:43 Comment(1)
Thank you very much :) The problem were, as you recommended the missing dependencies, and I didn't know how to export the runnable including these.Trehalose

© 2022 - 2024 — McMap. All rights reserved.