0

I need to use jackson to handle some JSON in my code. I wrote a simple test class to get it working.

import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
    public static void main(String[] args) { 
        ObjectMapper objectMapper = new ObjectMapper();
    }
}

I placed jackson-databind-2.9.9.1.jar in /dirwherejarresides/jdk/jre/lib/ext.

I am able to compile the class with no issues, and produce Test.class.

javac Test.java

However, I can't seem to get java find the jar & execute the class.

java -cp "/dirwhereclassresides/java" Test                                       Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
        at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:37)
        at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:291)
        at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
        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:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 3 more

I tried adding the path where the jar resides, but I get the same error.

java -cp "/dirwhereclassresides/java:/dirwherejarresides/jdk/jre/lib/ext/jackson-databind-2.9.9.1.jar" Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
        at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:37)
        at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:291)
        at Test.main(Test.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
        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:425)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 3 more

What do I need to do to get java to see the jar?

Acroyear
  • 1,354
  • 3
  • 22
  • 37

3 Answers3

1

Jackson library is split into several jar-modules that declare dependencies between each other. For example: jackson-databind depends on jackson-annotations and jackson-core.

The jackson-databind-*.jar does not actually contain a class for JsonView annotation that you get the exception about. It is placed in the jackson-annotations-*.jar file. But to be able to parse a JSON with Jackson you will definitely need jackson-core-*.jar as well.

Btw, your second way to pass a class path is more correct, you need to list all jar-files your class depends on in the -cp parameter split by colon on the Unix/Linux systems. E.g.

$ java -cp ".:/pathtojackson/jackson-databind-2.9.9.1.jar:/pathtojackson/jackson-core-2.9.9.jar:/pathtojackson/jackson-annotations-2.9.9.jar" Test

You can also use star-expressions in -cp parameter, see https://stackoverflow.com/a/219801/2288384

Vladimir L.
  • 1,116
  • 14
  • 23
1

It is complaining about annotation/JsonView and seems like it found jackson data-bind. Since you are not using maven, I suspect jackson-core which is a transitive dependency of data-bind, is not injected.

Try adding jackson-core into your classpath.

YoManTaMero
  • 391
  • 4
  • 10
0

Often ClassNotFoundException are a side effect of you having the two of the same packages with different versions. Print out a dependency tree and see that there is not two versions of the same dependency loaded.

user1478293
  • 46
  • 1
  • 5