1

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.

4
  • 1
    Did you add the jackson dependency in your POM.xml or build.gradle? Commented Jul 12, 2022 at 17:15
  • @BlaineWilsey I don't use a pom.xml or a build.gradle. I just added the libraries doing File -> Project Structure -> Librairies -> New Project Library and selecting my libraries from my lib folder Commented Jul 12, 2022 at 17:19
  • @user16320675 I said com.packagename,package because I have the same issue for whatever dependency I try to use, I just wanted to be generic. com.fasterxml.jackson.databind is my package indeed. Commented Jul 12, 2022 at 17:21
  • 1
    @BlaineWilsey I don't use a pom.xml or a build.gradle. I just added the libraries doing File -> Project Structure -> Librairies -> New Project Library and selecting my libraries from my lib folder -> Your libraries need to be the part of build. When you run your class from command line, you are probably not creating and executing the jar and therefore it doesn't know about your libraries. Either use maven/gradle for dependencies and then use it to build the project (jar) or lookup how to create and execute jar: baeldung.com/java-run-jar-with-arguments Commented Jul 12, 2022 at 20:27

1 Answer 1

0

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 ;!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.