1

I have recently explored handling JSON data with the org.json library and all went well. Now I started a bigger Maven project, for which I intend to use the Jackson libraries in stead. Sadly, it does not seem to work for me. I wanted to try out the ObjectMapper class, that VScode autocompleted for me, which also automatically adds the required import:

import com.fasterxml.jackson.databind.ObjectMapper;

However, I also immediately get an error on that line: "The type com.fasterxml.jackson.databind.ObjectMapper is not accessible Java (16778666)"

I have added the necessary dependencies to my pom.xml file like so:

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>13</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.14.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.14.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.0</version>
    </dependency>
</dependencies>

Am I missing something? Are there any other steps that I should have taken?

  • when you go from command line to the folder where you have your pom.xml and exectue `mvn clean package` what it shows? – Krzysztof Cichocki Dec 03 '22 at 12:30
  • [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project main: Compilation failure [ERROR] /C:/.../main/src/main/java/com/picit/DataHandler.java:[3,29] package com.fasterxml.jackson.databind is not visible [ERROR] (package com.fasterxml.jackson.databind is declared in the unnamed module, but module com.picit does not read it) – Ontkibbeling Dec 03 '22 at 14:36
  • I have to say I had assumed that maven comes automatically with JavaFX, as I could simply create a Maven project with VScode., but apparently it does not. I did not have maven installed apparently, as "mvn -v" initially was not recognized, so just now I download maven and added it to my classpath. I executed "mvn clean package" as shown above, but it did not help. I have the same error. – Ontkibbeling Dec 03 '22 at 14:37

2 Answers2

1

This is not a Maven problem. You probably have created a package-info.java for your project and so all your dependencies ended up on the module path but your package-info.java is missing the corresponding declarations.

You have to add a line like this:

requires com.fasterxml.jackson.databind;

See also: How to fix 'Package is declared in module, but module does not read it' error in IntelliJ JavaFX?

mipa
  • 10,369
  • 2
  • 16
  • 35
1

The only dependency needed for ObjectMapper is

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.0</version>
  </dependency>

This error occurs, because Jackson library is not included in your project's classpath. Probably your project is using the Java Platform Module System (JPMS); then log would also contain:

... declared in the "unnamed module" ... module does not read it.

If this is the case, add a requires directive to module-info.java file to specify that this module requires the jackson-databind library:

module correct.module.name {
  requires com.fasterxml.jackson.databind;
}

After adding this requires directive recompile the project again.

diziaq
  • 6,881
  • 16
  • 54
  • 96