I’m having problems resolving sub-dependencies from a third-party package. I’m a bit of a Maven beginner. Basically it goes like this:
git clone git://github.com/unidata/thredds.git
cd thredds
mvn install
Everything works great and stuff is installed into ~/.m2. Now, I wrote my own code that uses the package that I just installed:
<?xml version="1.0" encoding="UTF-8"?>
<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>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.1</version>
<name>Test Package</name>
<dependencies>
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.8-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Ok, this also works great when I run mvn package. The netcdf artifact is found. Now, when I try to run my code, I start down a long ClassNotFoundException path, having to keep adding all of netcdf‘s dependencies to my classpath.
Am I doing something wrong, or should the thredds package and all its dependencies automatically be picked up?
edit: the thredds package has many sub-modules one of which is netcdf. My code only depends on the netcdf jar.
edit: the snapshot version is installed
$ ls -l ~/.m2/repository/edu/ucar/netcdf/4.3.8-SNAPSHOT/
total 4272
-rw-rw-r-- 1 nwatkins nwatkins 700 2012-03-29 23:23 maven-metadata-local.xml
-rw-rw-r-- 1 nwatkins nwatkins 182 2012-03-29 23:23 _maven.repositories
-rw-rw-r-- 1 nwatkins nwatkins 4357494 2012-03-29 23:23 netcdf-4.3.8-SNAPSHOT.jar
-rw-rw-r-- 1 nwatkins nwatkins 7840 2012-03-29 22:28 netcdf-4.3.8-SNAPSHOT.pom
edit: to run the code which is in a single file Test.java
$ java -cp target/test-0.1.jar Test
edit: first error message
$ java -cp target/test-0.1.jar Test
Exception in thread "main" java.lang.NoClassDefFoundError: ucar/ma2/InvalidRangeException
Caused by: java.lang.ClassNotFoundException: ucar.ma2.InvalidRangeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
edit: then I just try to manually add the dependency jar to the class path
$ java -cp ../thredds/cdm/target/netcdf-4.3.8-SNAPSHOT.jar:target/test-0.1.jar Test
xception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at ucar.nc2.NetcdfFile.<clinit>(NetcdfFile.java:97)
at Test.main(Test.java:37)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
The slf4j package is also in ~/.m2. I stopped trying to add things to the classpath after this as it seemed like the wrong approach.
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Two solutions here (the cause of the problem has been explained in several answers):
1.Use maven exec plugin to launch your app:
2.You can also package your jar with all its dependencies, using maven assembly plugin. To do that, you need to add the following to your plugins of your build:
Then you can use the output jar created by maven assembly in your classpath arg