java.lang.NoClassDefFoundError when I add external JARs using Eclipse IDE

I am new to Java and Eclipse. I tried to add external jar files and import class in it but failed.

The code that import class in a jar file, new an instance and call a function of the object is shown below:

import cn.Hello;

public class Test {
    public static void main(String[] args) {
        Hello h = new Hello();

The Eclipse IDE doesn’t throw any error. But when I run with Eclipse, it threw:

Exception in thread "main" java.lang.NoClassDefFoundError: cn/Hello
    at Test.main(
Caused by: java.lang.ClassNotFoundException: cn.Hello
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
    at java.base/java.lang.ClassLoader.loadClass(
    ... 1 more

And the project I built on Eclipse is like this:

The project I built

Could you please help to solve it?


❯ find .

❯ jar tf /Users/fxb/Desktop/test.jar


Actually I figured out that I add the external jar file to the module path but not the class path. So the Eclipse IDE didn’t throw any error but at run time Java threw java.lang.NoClassDefFoundError. After I have added the jar to the class path, it’s OK. It’s in: right click the project -> Properties -> Java Build Path -> Libraries -> Classpath -> Add external JARs.