Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Not Found Exception for a Maven Jar

Tags:

java

maven

jar

I use maven to manage dependency in my project. This 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.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 package and try to run it from the terminal as java -jar test-fft.jar, I get a 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?

like image 412
Prasanna Avatar asked Sep 24 '14 10:09

Prasanna


2 Answers

<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

like image 163
Sunil Khokhar Avatar answered Nov 09 '22 00:11

Sunil Khokhar


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

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

like image 4
chenthil Avatar answered Nov 09 '22 01:11

chenthil