java Maven Jar 的类未找到异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26014840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:08:29  来源:igfitidea点击:

Class Not Found Exception for a Maven Jar

javamavenjar

提问by Prasanna

I use maven to manage dependency in my project. This is my pom.xml pom.xml

我使用 maven 来管理我的项目中的依赖项。这是我的 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.prasanna</groupId>
  <artifactId>fft-java</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>fft-java</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
      </dependency>
      <dependency>
          <groupId>com.github.wendykierp</groupId>
          <artifactId>JTransforms</artifactId>
          <version>3.0</version>
      </dependency>
  </dependencies>
    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.prasanna.TestFFT</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I have only one java class in my application. When I run my class from my application, it runs as expected. Whereas if I make a mvn packageand try to run it from the terminal as java -jar test-fft.jar, I get a java.lang.ClassNotFoundException: org.jtransforms.fft.DoubleFFT_2D

我的应用程序中只有一个 java 类。当我从我的应用程序运行我的类时,它按预期运行。而如果我制作 amvn package并尝试从终端运行它 as java -jar test-fft.jar,我会得到一个java.lang.ClassNotFoundException: org.jtransforms.fft.DoubleFFT_2D

But this DoubleFFT_2D is a part of JTransforms dependency that I have added. How would I run this jar?

但是这个 DoubleFFT_2D 是我添加的 JTransforms 依赖项的一部分。我将如何运行这个罐子?

回答by Sunil Khokhar

<build>
 <plugins>
 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>Mydir/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
             <version>2.4</version>
            <configuration>
             <outputDirectory>MyDir</outputDirectory>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib</classpathPrefix>
                        <mainClass>com.test.entryClassOfJarHavingMainMethod</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
 </plugins>
 
 
</build>

Use these configuration in pom.xml to create jar with lib folder and package as a jar

使用 pom.xml 中的这些配置创建带有 lib 文件夹的 jar 并打包为 jar

回答by chenthil

use the maven-assembly plugin to build the jar with dependency.

使用 maven-assembly 插件构建具有依赖关系的 jar。

   <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
  <execution>
    <phase>package</phase>
    <goals>
      <goal>single</goal>
    </goals>
  </execution>
</executions>
<configuration>
  <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
  </descriptorRefs>
</configuration>