"Error: Main class XXX not found or could not be loaded" occurs even though the main class exists

Event

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.

Cause

__ 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

"Error: Main class XXX not found or could not be loaded" occurs even though the main class exists
Error: Main class not found or could not be loaded
[Java] Main class XXX was not found or could not be loaded
If macOS Catalina says "Error: Main class XX was not found or could not be loaded"
Today's java error: Error: Could not find or load main class
HttpServletRequest class file not found
Error: Main class not found or could not be loaded
[Java] Main class XXX was not found or could not be loaded
java.util.Optional class and null and not null
Today's java error: Error: Could not find or load main class
If macOS Catalina says "Error: Main class XX was not found or could not be loaded"
"Error: Main class XXX not found or could not be loaded" occurs even though the main class exists
Symbol not found error when new class in another Java file
ld: framework not found Crashlytics workaround
Error in ./gradlew: Symbol not found
"Selection does not include main type"
ERROR! MySQL server PID file could not be found!
"Java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.FileSystems $ DefaultFileSystemHolder" occurs even though I can check the classpath and encoding many times.
The story that the Servlet could not be loaded in the Java Web application
Customize the display when an error such as 404 Not Found occurs in Spring Boot