Memcached dependency jar not working with maven project and throwing java.lang.ClassNotFound: net.spy.memcached.MemcachedClient Exception

StackOverflow https://stackoverflow.com/questions/16044437

Frage

My Project is in linux environment. I want to add memcached jar dependency jar in my project. So I have added following lines in pom.xml as described here.

  <repositories>
    <repository>
      <id>spy</id>
      <name>Spy Repository</name>
      <layout>default</layout>
      <url>http://files.couchbase.com/maven2/</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 <dependencies>
   <dependency>
        <groupId>spy</groupId>
        <artifactId>spymemcached</artifactId>
        <version>2.8.1</version>
        <scope>compile</scope>
    </dependency>
 </dependencies>

Program is successfully built. But whenever I am running the project jar, it throws following exception :

Exception in thread "main" java.lang.NoClassDefFoundError: net/spy/memcached/MemcachedClient
        at MemClient.<clinit>(MemClient.java:12)
        at MemClientUser.main(MemClientUser.java:18)
Caused by: java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 2 more

I have tried few solutions as given in following link :
Java ClassNotFoundException with maven dependency , but it didn't fix exception.
How can this exception be resolved?

War es hilfreich?

Lösung

It is not enough to run the jar, as Maven does not "incorporate" the dependencies of a module into the jar it is building. Either run the jar with

java -jar yourJar.jar -cp pathToTheSpymemcached.jar

where you replace yourJar.jar and pathToTheSpymemcached.jar appropriately or you may have a look at this: How can I create an executable JAR with dependencies using Maven?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top