32

This question is a follow up to this one. I can't seem to be able to access the jackson library in the following code:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServerConfiguration {
    public String info = null;
    public String idlURL = null;
    public String idlContents = null;
    public List<ServerInfo> servers = new ArrayList<>();

    public final void clear() {
        info = null;
        idlURL = null;
        idlContents = null;
        if (servers != null)
            servers.clear();
    }

    private final static ObjectReader jsonReader;
    private final static ObjectWriter jsonWriter;

    static {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // <== Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator class file for com.fasterxml.jackson.core.JsonGenerator not found
        //mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
        jsonWriter = mapper.writer();
        jsonReader = mapper.reader(ServerConfiguration.class);
    }

    public static ServerConfiguration fromJson(String json) throws IOException {
        return jsonReader.<ServerConfiguration>readValue(json); // <== Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException class file for com.fasterxml.jackson.core.JsonProcessingException not found
    }

    public String toJson() throws IOException {
        return jsonWriter.writeValueAsString(this);
    }

}

eventhough the jar files are in the classpath(autocomplete shows the method declaration in Intellij).

What am I missing?

Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • In my case the reason was that I had two different versions of Jackson libraries in the classpath. – Leukipp Jan 07 '16 at 22:26

6 Answers6

40

When I had this problem I had the jackson-annotations and jackson-databind jars in my classpath, but not jackson-core.

Adding jackson-core to the classpath solved it for me.

gregn3
  • 1,728
  • 2
  • 19
  • 27
  • 2
    If you're using Maven, also make sure that you use the correct `` value. This tells maven when and in some cases how it should resolve dependencies. Available values are `compile`, `provided`, `runtime`, `test`, `system` and `import`, you can read more about what they do here: http://stackoverflow.com/a/29230903/2804473 – Simon Cedergren Malmqvist May 24 '16 at 07:29
  • Adding this to the pom.xml fixed for me. Thanks for sharing your finding, this is based on that.: com.fasterxml.jackson.core jackson-core – SydMK Jul 04 '23 at 05:17
16

I was having a similar issue, it turned out to be an IntelliJ issue.

This helped me to resolve the issue:

Try File > Invalidate Caches > Invalidate and Restart.

If it doesn't help, delete .idea directory and reimport from pom/gradle build file.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
Ayush Jaiswal
  • 161
  • 1
  • 3
5

Also double check that all your Jackson dependencies use the same version like below :

implementation 'com.fasterxml.jackson.core:jackson-core:2.10.1'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.10.1'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.10.1'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.10.1'

I previously had jackson-core:2.11.0 and this was the issue.

2

I also encountered this kind of error: Cannot access com.fasterxml.jackson.core.TreeNode

My first version of the maven POM contained:

  • jackson-annotations
  • jackson-databind

It was working fine until I wanted to add jackson-core for unit tests. I added it with the test scope and the error appeared. After some investigation, I found out that to make it working, I had to remove the test scope, otherwise this error appears. If someone has an explanation I would be interested :)

Chavjoh
  • 466
  • 5
  • 13
0

I had also encountered the same problem, turns out, I had "jackson-core-asl.jar" instead of "jackson-core.jar"

So, check whether you have this jar-file in your classpath. -> https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core

Yash P Shah
  • 779
  • 11
  • 15
0

1 go to the .m2/repository and delete the conflicting files

2 mvn -U clean install

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40