0

I am having an issue running a JAR that I have packaged using Maven. It is a very simple class that just takes a file from a directory on the local machine and posts it to an SFTP.

I am using the "mvn package" command and am seeing the JAR show up in my target folder, but when I attempt to run the JAR I am getting the following error:

"Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/Jsch at. java.lang.Class.getDeclaredMethods0 etc.

Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.Jsch"

Now the program runs just fine when I run it in eclipse just running the main method, so I assume that it is something with the maven package command not bringing all the correct classes into my JAR? In my "Maven Dependencies" all I have is the "jsch-0.1.49.jar" and "junit-3.8.1.jar" Any help would be greatly appreciated. I am sure there is a step I missed somewhere in this process.

parchambeau
  • 1,141
  • 9
  • 34
  • 56

1 Answers1

1

The maven package command and the maven-jar-plugin did not build jar files with the dependencies attached. They just build a jar file with your project sources.

You need an uber jar which consist of everything your main class need. The easiest way to archive this is by using the maven-shade-plugin:

<project>
   ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>
mszalbach
  • 10,612
  • 1
  • 41
  • 53
  • Awesome answer, gained understanding of maven, worked perfectly. Couldn't have asked for anything better. Thank you. – parchambeau May 16 '13 at 17:18