Home > OS >  How to fix "error: package com.fasterxml.jackson.databind does not exist" when compiling u
How to fix "error: package com.fasterxml.jackson.databind does not exist" when compiling u

Time:07-16

I have been trying to compile my Java code using the format javac Main.java but for some reason the compiler says that my package does not exist and as a matter of fact it is in the project structure, here is a screenshot:

Files structure of my code

The exact error is: Main.java:1: error: package com.fasterxml.jackson.databind does not exist import com.fasterxml.jackson.databind.ObjectMapper;

And my code looks like this in my Main.java:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;


public final class Main {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("Usage: Main [file path]");
            return;
        }

        UdacisearchClient client =
                new UdacisearchClient(
                        "CatFacts LLC",
                        17,
                        8000,
                        5,
                        Instant.now(),
                        Duration.ofDays(180),
                        ZoneId.of("America/Los_Angeles"),
                        "555 Meowmers Ln, Riverside, CA 92501");

        Path outputPath = Path.of(args[0]);
        
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.writeValue(Files.newBufferedWriter(outputPath), client);

        System.out.println("Wrote to: "  outputPath.toAbsolutePath());
        UdacisearchClient deserialized = objectMapper.
                readValue(Files.newBufferedReader(outputPath), UdacisearchClient.class);

        System.out.println("Deserialized: "   deserialized);

    }
}

The whole code is supposed to compile like this javac Main.java and then java Main client.json. When I try to compile it by going to Run, Edit Configurations and by adding client.json as the argument of my program it works like a charm, my object is serialized as a json object in the client.json file but when I compile using command line it says no package is found. The same error happens for any other dependency I try to use. It should be noted that when I instantiate objects from my dependency it looks fine as the import lines related to those objects aren't red. So I guess my issue resides in my command line compilation or my Intellij environment. I have tried many of the solution proposed online but the problem remains. I would like some help please.

CodePudding user response:

It turns out the solution was simple.

First compiling the libraries inside the lib folder and Main.java doing :

javac -cp ".;lib/*" Main.java

Then running my class Main (containing my main function):

java -cp ".;lib/*" Main

I was missing on the dot "." and the semicolon ;!

  • Related