【问题标题】:Maven package with Eclipse Project as dependency leads to NoClassDefFoundError以 Eclipse 项目为依赖项的 Maven 包导致 NoClassDefFoundError
【发布时间】:2018-11-02 09:50:28
【问题描述】:

情况是这样的。 我正在为(历史上)基于 CLI 的进程开发一个 GUI 客户端。这些过程都在 Eclipse 中的一个 Maven 项目下。我的 GUI 是一个单独的 Eclipse Maven 项目。 GUI 项目在其 pom.xml 中引用了其他项目,mvn package 不会失败,但是当我运行项目的 ...-jar-with-dependencies.jar 时,我会得到一个孩子项目类的 NoClassDefFoundError

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>proto-xml-validator</artifactId>
  <organization>
    <name>SGcib</name>
    <url>https://cib.societegenerale.com/en/</url>
  </organization>
  <build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui</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>
  </plugins>
</build>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/com.miglayout/miglayout-swing -->
<dependency>
    <groupId>com.miglayout</groupId>
    <artifactId>miglayout-swing</artifactId>
    <version>5.0</version>
</dependency>

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
  </dependency>
  <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>idea</groupId>
      <artifactId>ideaToolsUtils</artifactId>
      <version>1.2-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>bacardi</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <artifactId>pnl-loader</artifactId>
    </dependency>
</dependencies>
  <groupId>bacardi</groupId>
  <version>0.0.1-SNAPSHOT</version>
</project>

这是堆栈跟踪

$ java -jar target/proto-xml-validator-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: idea/file/loader/runner/ValuationBVLoaderRunner
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.initialize(ProtoXMLValidatorGui.java:55)
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.<init>(ProtoXMLValidatorGui.java:33)
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui$1.run(ProtoXMLValidatorGui.java:127)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: idea.file.loader.runner.ValuationBVLoaderRunner
        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)

触发NoClassDefFoundError 的类位于bacardi groupId 中的pnl-loader artifactId 下。

我已经将项目依赖项的范围设置为compile

有谁知道我做错了什么?

提前致谢。

【问题讨论】:

  • 您是否为pnl-loader 项目运行了mvn clean install?另请发布错误堆栈。
  • 使用堆栈跟踪编辑。
  • idea.file.loader.runner.ValuationBVLoaderRunner 应该位于哪里?
  • 项目中pnl-loader包中idea.file.loader.runner
  • 为 pnl-loader 项目运行 mvn clean install 然后为 proto-xml-validator 运行 mvn clean package

标签: java eclipse maven dependencies


【解决方案1】:

解决我的问题:

问题是,尽管我的 Eclipse Maven 项目具有正确的依赖项引用,但它无法将它们正确地打包到 JAR 中。事实上,项目依赖对 Maven 没有用(它不知道 Eclipse 项目是什么,ofc)。 Maven 在他的存储库中寻找已编译的 JAR。这就是@aka-one 在 cmets 中指出的。为了最终打包我的项目,我必须 mvn clean install 构建所需的几个项目。

【讨论】:

    猜你喜欢
    • 2011-04-15
    • 2020-07-28
    • 2018-10-28
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2013-11-25
    相关资源
    最近更新 更多