Java running error: Error: Could not find or load main class

created at 10-17-2021 views: 3

Compile HelloWorld.java to generate the bytecode file HelloWorld.class, and run HelloWorld.class to report an error: Error: Could not find or load main class.

Cause Analysis

The current directory is the directory where the .class file is located. To run the .class bytecode file, you need to add the current directory to the system variable CLASSPATH so that the system can find the Java class in the current directory.

The classpath path is entered incorrectly, you need to enter the current path and add .: in front of the original path

solution

step 1: Add the CLASSPATH system variable at the end of the profile file under /etc .:, . represents the current path.

The CLASSPATH in the original /etc/profie file is:

 CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

The increased CLASSPATH is:

 CLASSPATH=.:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

Step 2: Enter source /etc/profie on the command line to make the system variables in /etc/profile take effect, and you can run it.

created at:10-17-2021
edited at: 10-17-2021: