user972590
user972590

Reputation: 251

Unable to execute maven generated jar file using java -jar

Hi I am using maven2 to build my project. I am able to generate the jar file using maven build with the command mvn clean install.

I have added this plugin to my pom.xml for manifest file to make the class path entry and main class to execute:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
       <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
               <mainClass>com.test.TestExample</mainClass>

            </manifest>
          </archive>
        </configuration>     
      </plugin>

When I build the project, and extracted the jar file, its generated manifest.mf file and added the main class entry as : Main-Class: com.test.TestExample and added the jar files to the Class-Path:mail-1.4.jar. But when I am tryign execute the jar file using command java -jar TestJar.jar I am getting the exception :

Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Any pointer to solve this is very helpful..

Thanks in advance..

Upvotes: 2

Views: 1856

Answers (1)

Raghuram
Raghuram

Reputation: 52625

What you want is to indicate the location of the dependencies to the executable jar. You could try updating your plugin configuration as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
   <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
           <classpathPrefix>lib/</classpathPrefix>
           <mainClass>com.test.TestExample</mainClass>
        </manifest>
      </archive>
    </configuration>     
  </plugin>

Then ensure that the lib folder containing your dependencies are present at the same level as your executable jar.

Upvotes: 1

Related Questions