Java Running Error: Could not find or load main class

[problem description]

User root is created in the/root/exercise folder HelloWorld.java The code is as follows:

Compile and run the file as follows:

Compile HelloWorld.java Generate bytecode file HelloWorld.class , running HelloWorld.class 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 to find the Java class in the current directory.

 

[solution]

Step 1: add the classpath system variable at the end of the profile file in/etc.:,. To represent the current path.

The classpath in the original/etc/profile file is:

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

The added classpath is: classpath =.: $Java_ HOME/jre/lib/ext:$JAVA_ HOME/lib/ dt.jar :$JAVA_ HOME/lib/ tools.jar

Step 2: input source/etc/profile on the command line to make the system variables in/etc/profile effective and then run.

Note: you can modify system variables only if the current user is root. If you enter source/etc/profile on the command line, the system variable will only take effect temporarily. If you close terminal, the system variable will be restored to its original state. Because the system variable is loaded at system startup, the system variable will be loaded automatically only at the next system restart, so you don’t need to enter source/etc/profile again.

The results are as follows


Read More: