First, suppose the following Child class exists.
Child.java
public class Child extends Parent {
public static void main(String[] args) {
System.out.println("This class is `Child`");
}
}
The result of storing this in child.jar and executing it in Java 8 is as follows.
C:\>java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
C:\>java -cp child.jar Child
error:Main class Child was not found or could not be loaded
Java execution failed with the error "Main class Child was not found or could not be loaded" even though the Child class does exist.
__ The real reason is that the parent class Parent of Child cannot be read. __ Actually, Parent is stored in parent.jar, which is different from child.jar. However, parent.jar cannot be read by the above java command execution method.
So I added parent.jar to the classpath and ran java, and it worked as expected.
C:\>java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)
C:\>java -cp child.jar;parent.jar Child
This class is `Child`
Looking only at the "Child not found" part, it seems that Child was not stored correctly in child.jar, and in fact I wasted a lot of time because of this belief. ……. It's bad that the error message is hard to understand (reverse giraffe)
Recommended Posts