Issue
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();
h.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(Test.java:5)
Caused by: java.lang.ClassNotFoundException: cn.Hello
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
And the project I built on Eclipse is like this:
Could you please help to solve it?
p.s
❯ find .
.
./.DS_Store
./bin
./bin/Test.class
./.classpath
./.gitignore
./.project
./src
./src/Test.java
❯ jar tf /Users/fxb/Desktop/test.jar
META-INF/MANIFEST.MF
cn/Hello.class
cn/edu/Hello.class
Solution
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
.
Answered By - Y. Z. FENG