Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven executable Jar throws error on start [duplicate]

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 :)

like image 427
Fabian Schneider Avatar asked Feb 09 '17 06:02

Fabian Schneider


1 Answers

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.

like image 55
gybandi Avatar answered Oct 02 '22 14:10

gybandi