Maven可执行文件Jar在启动时引发错误 [英] Maven executable Jar throws error on start

查看:71
本文介绍了Maven可执行文件Jar在启动时引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我是Maven的新手.我制作了第一个Maven应用程序,并在IDE中成功对其进行了测试.构建总是成功的,并且一切都像魅力一样.

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.

现在我想将项目导出为具有内置依赖项的可执行jar,但是我不确定为什么它不起作用.

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.

我在pom文件中添加了以下内容,因为这是我在类似问题的各种答案中找到的

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>

我知道这指定了JVM启动的主类,因为IDE不会自动设置它.

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

我将jar放在目标目录中,将其复制到另一个目录并尝试执行.

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

可悲的是,抛出了以下错误:

Sadly the following errors are thrown:

您能给我一个提示,我可能在哪里出错了?那很好啊.(如果有帮助,我正在使用NetBeans.)

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

这是我的StackTrace:

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

谢谢:)

推荐答案

如果我是正确的,则maven-jar-plugin将使用所有已编译的.class文件创建一个jar,但没有依赖项.

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

我建议使用 maven-assembly-plugin 并将其绑定到软件包执行阶段,这样,当运行 mvn install

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.

这篇关于Maven可执行文件Jar在启动时引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆