Error: Could not find or load main class ***

Question:
Javac compiles the source code and executes the Java Mainclassname Error: Could not find or Load main class *** Error
The reason:
The runtime cannot find the main class, either because it did not write the full name of the main class (it requires the full package name) or because it wrote the wrong name of the main class (not the name of the main class, but the file name of the main class), or because it did not specify a directory with the classpath.
Solution 1:
Java classpath – [classpath directory] package1, package2. Mainclassname
Such as:
Run the command as in ***/target/classes/hello directory
java -cp ../hello/HelloWorld
or
java -cp ../hello. The HelloWorld
(-classpath here can also be -cp.)
Note that the path after 1: -classpath is in the directory of the top-level package name (package1) and in the directory of the package1 folder (if the source program does not start with any packages, that is the current compiled directory, -cp./ The current path can be without specifying the -cp parameter), or.. Relative or absolute paths to /target/classes/
Note 2: run the file must be full name, to add all the package name (if you have any package program starts packagename) until the main class name, such as: packagename. Mainclassname (note that there is the main class name for the file name is not the main class, also is not generated by the * * *. The class files, is the main method in the name of the class)
Solution 2
The directory from CD to the classpath, if there is a package name (the directory where the top-level package name package1 folder is located, or.. If the source program does not have a package at the beginning, run the following command in the path of the ***.class file after compilation.
Java package1. Package2. Mainclassname
Such as:
Run the following command in the ***/target/classes directory where the package name hello resides
Java hello/HelloWorld
or
Java hello. The HelloWorld
Conclusion:
The problem is that the main class cannot be found, it must run with the full main class name including the package name, and it needs to specify the directory of the class path.
Appendix:
Java usage methods and parameters table after running Java-help
Usage: java [-options] class [args…]
(to execute a class)
or Java [-options] -jar jarfile [args…]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to Select the “server” VM
the default VM is server.
– cp & lt; class search path of directories and zip/jar files>
– the classpath & lt; class search path of directories and zip/jar files>
A: separated list of directories, JAR archives,
and ZIP archives to search for class files. name> =< value>
set a system property
-verbose :[class|gc|jni]
enable verbose output
-version print product version and exit
-version :< value>
Warning: This feature is deprecated and will be removed
in a future release.
requires the specified version to run
-showversion print product version and Restrict-search continue
-re-search restricb-no-re-search
Warning: continue
-re-search restricb-search Removed
in a future release.
include/exclude user private JREs in the version search
-?-help print this help message
-x print help on non-standard options
-ea [:<packagename>…|:<classname> classname>] [: & lt;
– enableassertions packagename>… | : & lt; classname>]
enable assertions with specified granularity
-da [:<packagename>…|:<classname>] [: & lt;
the -disableassertions packagename>… | : & lt; classname>]
disable assertions with specified coverage
-esa | -enablesystemassertions
enablesystem enabling
-dsa |-disablesystemassertions
Disable the system assertions
– agentlib: & lt; libname> [=<options>]
load native agent library < libname> -agentlib:hprof
see also, -agentlib: JDWP =help and -agentlib:hprof=help
-agentpath :< pathname> [=<options>]
load native agent library by full pathname
-javaagent :< jarpath> [=<options>]
load Java programming language agent, see java.lang. Instrument
-splash :< imagepath>
show splash screen with the specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Helloworld.java :(notice the package name)

package hello;

public class HelloWorld {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
    }
}

Greeter.java

package hello;

public class Greeter {
    public String sayHello() {
        return "Hello world!";
    }
}

Reference:
https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean

Read More: