[英]How to fix "error: package com.fasterxml.jackson.databind does not exist" when compiling using command line in Intellij (Java)
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:我一直在尝试使用
javac Main.java
格式编译我的 Java 代码,但由于某种原因,编译器说我的包不存在,事实上它在项目结构中,这是一个屏幕截图:
The exact error is: Main.java:1: error: package com.fasterxml.jackson.databind does not exist import com.fasterxml.jackson.databind.ObjectMapper;
确切的错误是:
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:我的代码在我的 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
.整个代码应该像这样
javac Main.java
然后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.当我尝试通过转到
Run
, Edit Configurations
并通过添加client.json
作为我的程序的参数来编译它时,它就像一个魅力,我的对象在client.json
文件中被序列化为一个 json 对象但是当我使用编译时命令行它说没有找到包。 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.应该注意的是,当我从我的依赖项中实例化对象时,它看起来很好,因为与这些对象相关的
import
行不是红色的。 So I guess my issue resides in my command line compilation or my Intellij environment.所以我想我的问题在于我的命令行编译或我的 Intellij 环境。 I have tried many of the solution proposed online but the problem remains.
我已经尝试了许多在线提出的解决方案,但问题仍然存在。 I would like some help please.
我需要一些帮助。
It turns out the solution was simple.事实证明,解决方案很简单。
First compiling the libraries inside the lib folder and Main.java doing :首先编译lib文件夹和Main.java中的库:
javac -cp ".;lib/*" Main.java
Then running my class Main (containing my main function):然后运行我的Main类(包含我的main函数):
java -cp ".;lib/*" Main
I was missing on the dot " . " and the semicolon ;我缺少点“ . ”和分号; !
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.