简体   繁体   English

具有本地依赖项的 maven 项目中的 NoClassDefFoundError

[英]NoClassDefFoundError in maven project with local dependency

I'm trying to run.jar file created by maven with one local dependency (processing library) that is not being recognized.我正在尝试运行由 maven 创建的 .jar 文件,其中包含一个未被识别的本地依赖项(处理库)。 I used maven-install-plugin.我使用了 maven-install-plugin。 Library's jar is located in root/lib.库的 jar 位于 root/lib。 After running运行后

$ mvn validate
$ mvn clean package
$ java -cp target/my-app-1.0.jar com.mycompany.app.App

I get error:我收到错误:

Error: Could not find or load main class com.mycompany.app.App
Caused by: java.lang.NoClassDefFoundError: processing/core/PApplet

However the app works fine when I run:但是,当我运行时,该应用程序运行良好:

$ java -cp target/classes:$HOME/.m2/repository/org/processing/core/10/core-10.jar com.mycompany.app.App

Here is my pom.xml:这是我的 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>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>14</maven.compiler.source>
    <maven.compiler.target>14</maven.compiler.target>
  </properties>

  <dependencies>
    <!--  LOCAL  -->
    <dependency>
      <groupId>org.processing</groupId>
      <artifactId>core</artifactId>
      <version>10</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <groupId>org.processing</groupId>
          <artifactId>core</artifactId>
          <version>10</version>
          <packaging>jar</packaging>
          <file>${pom.basedir}/lib/processing-core.jar</file>
          <generatePom>true</generatePom>
        </configuration>
        <executions>
          <execution>
            <id>install-jar-lib</id>
            <goals>
              <goal>install-file</goal>
            </goals>
            <phase>validate</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
</project>

And here is source:这是来源:

package com.mycompany.app;

import processing.core.PApplet;

public class App extends PApplet{
    public static void main( String[] args ) {
        PApplet.main(App.class, args);
        System.out.println( "Hello World!" );
    }
}

My environment:我的环境:

$ mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 14.0.2, vendor: Private Build, runtime: /usr/lib/jvm/java-14-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.8.0-33-generic", arch: "amd64", family: "unix"

Thank you for all suggestions.谢谢你的所有建议。

The maven-install-plugin is used to install an artifact in your local maven repository. maven-install-plugin 用于在本地 maven 存储库中安装工件。 This is not what you want to do here.这不是您想在这里做的。 Your goal is to get a dependency in the classpath.您的目标是在类路径中获得依赖项。

To be able to start your code with this command为了能够使用此命令启动您的代码

java -cp target/my-app-1.0.jar com.mycompany.app.App

you have to ensure two things你必须确保两件事

  1. The application jar's manifest must contain the path of the dependency jar应用程序 jar 的清单必须包含依赖项的路径 jar
  2. The dependency jar has to be at the specified location依赖项 jar 必须位于指定位置

For the first part, you can use the maven-jar-plugin :对于第一部分,您可以使用maven-jar-plugin

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

Be aware that this will add all dependencies to the classpath, which might or might not be what you want.请注意,这会将所有依赖项添加到类路径中,这可能是也可能不是您想要的。

For the second part, you can use the maven-dependency-plugin .对于第二部分,您可以使用maven-dependency-plugin This will copy all dependencies (or with appropriate configuration only selected dependencies) to target/lib这会将所有依赖项(或通过适当的配置仅选择依赖项)复制到 target/lib

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
    </configuration>
</plugin>

With this configuration, the target directory should look somewhat like this, showing only relevant files/directories:使用此配置,目标目录应该看起来像这样,仅显示相关文件/目录:

target
|- lib
|  |- core-10.jar
|- my-app-1.0.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM