Maven executable Jar throws error on start [duplicate]

不想你离开。 提交于 2021-02-08 14:25:09

问题


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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/42129639/maven-executable-jar-throws-error-on-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!