jackson 如何使用命令行和shell脚本使java类路径正常工作?

xmd2e60i  于 2022-11-08  发布在  Shell
关注(0)|答案(1)|浏览(193)

我试图使用jackson-databindObjectMapper(不使用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
tf7tbtn2

tf7tbtn21#

在运行时使用相同的类路径,只是现在还需要查找First
因此:
java -cp ".:./jackson-databind-2.13.3.jar" First
. =当前目录(以便在解析完全限定类名First时找到First.class)。
:分隔类路径条目
./jackson-databind-2.13.3.jar,这将意味着解析fqn com.fasterxml.jackson.databind.ObjectMapper将找到正确的类。

相关问题