2

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

5
  • Are you sure? Works as-is for me. You're unzipping the jar file and checking for the manifest? The plugin is in the builds/plugins element? Commented Nov 22, 2011 at 5:23
  • yes Dave, I have edited my question. Please have a look. Commented Nov 22, 2011 at 5:28
  • I have added the dependency in the pom.xml for mail.jar and its in my local path D:\jars\mail.jar, I am running through the eclipse, and added the entry into build path for mail.jar using the local path, now i am trying to run through the jar file, its unable to find the mail.jar. How can make the mail.jar file availabe to the jar which i am trying to execute? Commented Nov 22, 2011 at 5:42
  • By putting it on the classpath specified in the manifest file. Commented Nov 22, 2011 at 5:44
  • I have a local folder called lib, in which i have all the jars like mail.jar. Can i use that reference to make the mail.jar available? Commented Nov 22, 2011 at 5:47

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Raghuram, i have a classpath D:/eclipseWS/TestProj/src/main/resources/lib/xstream-1.2.2.jar"/, and i mentioned <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix>, but i am unable to find the jar files from the lib folder.. thanks for your time
You had commented that you had a local folder called lib where you had all these jars. My answer was in response to that. The above snippet will not put any jars anywhere

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.