2

I have defined jersey service as:

private static HttpServer createHttpServer() throws IOException {
    final ResourceConfig resourceConfig = new ResourceConfig(Api.class);

    return JdkHttpServerFactory.createHttpServer(URI, resourceConfig);

}

This compiles fine. But when I try to run it it throws java.lang.NoClassDefFoundError:

        Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/server/ResourceConfig
        at EmbeddedHTTPServer.createHttpServer(Unknown Source)
        at EmbeddedHTTPServer.main(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.server.ResourceConfig
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

These are the libs:

$ ls lib
commons-collections-3.2.1.jar  jsoup-1.8.3.jar
commons-dbcp-1.4.jar           logback-classic-1.1.3.jar
commons-email.jar              logback-core-1.1.3.jar
commons-io-2.4.jar             mail.jar
commons-lang3-3.0.1.jar        mysql-connector-java-5.1.17.jar
gdata-core.jar                 quartz-2.2.1.jar
javaee-web-api.jar             rome-1.5.1.jar
jdom2-2.0.6.jar                rome-modules-1.5.1.jar
jersey-container-jdk-http.jar  rome-utils-1.5.1.jar
jersey-server.jar              slf4j-api-1.7.12.jar
json.jar

Env:

  1. Ubuntu
  2. Oracle JDK 1.8
  3. JAX-RS 2

Any idea?

Thanks.

Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37
  • You're missing a whole bunch of jars. Download the [distribution bundle](https://jersey.java.net/download.html), and add all the jars. – Paul Samsotha Feb 11 '16 at 00:40
  • @peeskillet I tried adding each of the jars (jersey-client.jar, jersey-media-jaxb.jar, jersey-common.jar, jersey-container-servlet-core.jar, jersey-container-servlet.jar) in the distribution but this did not resolve the issue. – Mugoma J. Okomba Feb 11 '16 at 00:48
  • _All_ the jar? And remove your current jersey-server. You need to add all the jars from that distribution. And make sure all the jars are on the classpath when you run the main jar. You can look at the manifest to see if they are listed or you can manually cp them – Paul Samsotha Feb 11 '16 at 00:51
  • When I remove jersey-server.jar it can't compile. Gives `error: package org.glassfish.jersey.server does not exist` – Mugoma J. Okomba Feb 11 '16 at 01:16
  • I'm saying remove the current one you have and add the one from the bundle. Your main problem is a classpath problem. You need to make sure all the jars are on the classpath when you load the app. – Paul Samsotha Feb 11 '16 at 01:18
  • Other than `jersey-container-jdk-http.jar` I don't have jeysey jars from other sources. All of them are from https://jersey.java.net/download.html – Mugoma J. Okomba Feb 11 '16 at 05:21
  • Ok you need to add all those. _And_ you need to make sure they are all on the classpath. If you are unsure what this means, then you should do some research. Just putting the jars in a lib folder, does not put them on the class path. A NoClassDefFoundError, means that the class that is being used in your code, i.e the `ResourceConfig` is not loaded, most likely because the jar is not on the class path – Paul Samsotha Feb 11 '16 at 05:24
  • Do I also need to use jars from `ext`? I only added ones in `lib`. – Mugoma J. Okomba Feb 11 '16 at 05:25
  • Add _all_ of them. But even after that, you still need to solve your classpath problem – Paul Samsotha Feb 11 '16 at 05:26
  • I am using `ant` to compile and it includes everything in `lib` – Mugoma J. Okomba Feb 11 '16 at 05:27
  • Does it also add a Class-Path attribute in your manifest pointing to all those jars in the lib folder. If not then they are _not_ automatically on the classpath, and you need to explicitly -cp them yourself. I don't work with Ant (manually), so if the Class-Path is not set in the Manifest, I couldn't tell you how to do it. If ever I work with Ant, my IDE (Netbeans) automatically set the Class-Path for me. First thing should do is look at the MANIFEST in your jar for that attribute – Paul Samsotha Feb 11 '16 at 05:30
  • Resolved by adding jars from ext folder – Mugoma J. Okomba Feb 11 '16 at 07:52

1 Answers1

1

It appears Jersey compiles properly with only jars in lib folder but at runtime jars in ext folder are required.

After adding jars in ext folder from the JAX-RI bundle issue resolved.

Mugoma J. Okomba
  • 3,185
  • 1
  • 26
  • 37