Java error: unable to find or load main class (package name in source file)

1. Problem orientation

This type of error occurs when compiling (javac) and executing (Java) Java programs: the main class cannot be found or loaded:

First of all, the problem caused by improper configuration of environment variables is excluded. As long as the command line interface can recognize javac / Java commands, there is no problem with the configuration of environment variables. This problem often occurs because there is a package name in the Java source file, such as the file C:: code\ Hello.java :

package com.example;

public class Hello{
    public static void main(String[]args){
        System.out.println("Hello");
    }
}

It seems that there is no problem

C:\code>javac Hello.java
C:\code>java Hello
Error: Main class not found or could not be loaded Hello

2. Solutions

Delete the package name from the source file (not recommended); create a file path structure with the same package name under code (C:: – Code ⁃ com ⁃ example)\ Hello.java )

Compilation:C:\code>javac com/example/Hello.java
Run:C:\code>java com.example.Hello

Read More: