我试图使用jackson-databind
的ObjectMapper
(不使用maven),但无法将相应的ObjectMapper
类集成到我的程序中。
我的文件结构:
.
├── First.class
├── First.java
├── jackson-databind-2.13.3.jar
├── run.sh
└── this.json
第一个.java:
import com.fasterxml.jackson.databind.ObjectMapper;
public class First {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
System.out.println("Hello world");
}
}
运行.sh:
echo "Compiling..."
javac -cp "./jackson-databind-2.13.3.jar" First.java
echo "Compiled."
echo "----------------"
echo "Output:"
java First
出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at First.main(First.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 1 more
我做错了什么?请帮帮我
更新
我将**run.sh
**文件更改为:
echo "Compiling..."
javac -cp ".:./jackson-databind-2.13.3.jar" First.java
echo "Compiled."
echo "----------------"
echo "Output:"
java -cp ".:./jackson-databind-2.13.3.jar" First
并得到以下消息:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/Versioned
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at First.main(First.java:5)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.Versioned
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 10 more
1条答案
按热度按时间tf7tbtn21#
在运行时使用相同的类路径,只是现在还需要查找
First
。因此:
java -cp ".:./jackson-databind-2.13.3.jar" First
个.
=当前目录(以便在解析完全限定类名First
时找到First.class
)。:
分隔类路径条目./jackson-databind-2.13.3.jar
,这将意味着解析fqncom.fasterxml.jackson.databind.ObjectMapper
将找到正确的类。