【问题标题】:"Java error occurred" message while executing a jar built from maven执行从 Maven 构建的 jar 时出现“Java 错误”消息
【发布时间】:2015-07-08 03:47:11
【问题描述】:

我正在尝试构建一个可执行的 jar。当我通过 Eclipse 从项目中执行代码时,它运行良好,但当从 CMD 行执行 jar 时却不行。看起来类路径有些问题,但我不确定......我的 pom 文件中有以下依赖项:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.12</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.12</version>
    <scope>compile</scope>
</dependency>

以及pom文件标签中的以下maven插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.swx.nursing.tools.quicklaunch.executor.QuickLaunch</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

然后我使用以下内容来构建 jar: mvn 干净安装

当我执行 jar 时,我看到以下消息,我不确定为什么:

C:\Workspaces\CCQueryHotkey\quicklaunch\target>java -jar quicklaunch-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.swx.nursing.tools.quicklaunch.executor.QuickLaunch.<clinit>(QuickLaunch.java:18)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

我看到 SLF4j jar 在 maven 拉取的项目文件夹中。

请指教!

谢谢

【问题讨论】:

    标签: java maven manifest pom.xml executable-jar


    【解决方案1】:

    您可以使用 shade 插件来创建一个可执行的 jar。 以this link为例

    【讨论】:

      【解决方案2】:

      您必须将类路径设置为您的 .m2 存储库。尝试执行

      java -cp "Path to your .m2 repository." -jar quicklaunch-0.0.1-SNAPSHOT.jar
      

      【讨论】:

      • 这不起作用,它给了我一条消息,说缺少主类。我也在尝试构建一个可执行的 jar,而不仅仅是从 CMD 运行它
      【解决方案3】:

      您收到 java.lang.NoClassDefFoundError,因为类在运行时在类路径位置不可用。

      您可以使用 maven 程序集插件来解决此问题。

      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>1.5</source>
              <target>1.5</target>
          </configuration>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
          <!-- nothing here -->
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-4</version>
          <configuration>
              <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
      
                  <finalName>test</finalName>
                  <appendAssemblyId>false</appendAssemblyId>
                  <archive>
                      <manifest>
                          <mainClass>org.swx.nursing.tools.quicklaunch.executor.QuickLaunch</mainClass>
                      </manifest>
                  </archive>
          </configuration>
          <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                      <goal>single</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      

      使用上面给出的配置更新您的 pom.xml 并运行 mvn clean install。然后,cd 进入目标目录,然后重试:

      java -jar test.jar

      供参考:maven assembly plugin

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-21
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 2018-07-16
        • 2010-12-21
        相关资源
        最近更新 更多