java Maven Jar的类未找到异常

zqdjd7g9  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(190)

我使用maven来管理项目中的依赖关系。这是我的pom.xmlpom.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>

我的应用程序中只有一个java类,当我从应用程序中运行类时,它会按预期运行,而如果我创建一个mvn package,并尝试从终端以java -jar test-fft.jar的身份运行它,则会得到java.lang.ClassNotFoundException: org.jtransforms.fft.DoubleFFT_2D
但是这个DoubleFFT_2D是我添加的JTransforms依赖项的一部分,我该如何运行这个jar呢?

svujldwt

svujldwt1#

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

在pom.xml中使用这些配置创建包含lib文件夹的jar,并将其作为jar包

scyqe7ek

scyqe7ek2#

使用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>

相关问题