Home > Software engineering >  How do I get my java classpath to work using the commandline and shell scripts?
How do I get my java classpath to work using the commandline and shell scripts?

Time:07-28

I am trying to use jackson-databind's ObjectMapper (without using maven) but cannot integrate the corresponding ObjectMapper class into my program.

My file structure:

 .
├──  First.class
├──  First.java
├──  jackson-databind-2.13.3.jar
├──  run.sh
└──  this.json

First.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");
    }
}

run.sh:

echo "Compiling..."
javac -cp "./jackson-databind-2.13.3.jar" First.java
echo "Compiled."
echo "----------------"
echo "Output:"
java First

I get the following error:

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

What am I doing wrong? Please help

UPDATE

I changed the run.sh file to this:

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

and got the following message:

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

CodePudding user response:

Same classpath at runtime, except, you now need to find First as well.

Thus:

java -cp ".:./jackson-databind-2.13.3.jar" First

. = current dir (so that First.class will be found when resolving fully qualified classname First.

: separates classpath entries

./jackson-databind-2.13.3.jar which will mean that resolving fqn com.fasterxml.jackson.databind.ObjectMapper will find the right class.

  • Related