Maven Jar没有找到类别

时间:2014-09-24 10:41:01

标签: java maven jar

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

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

但是这个DoubleFFT_2D是我添加的JTransforms依赖项的一部分。我该怎么办这个罐子?

2 个答案:

答案 0 :(得分:3)

<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

答案 1 :(得分:2)

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