When running a Java program in the cmd window, the solution of the main class cannot be found.
I am a beginner in Java. I encountered a problem when running a Java program in the cmd window yesterday. It can be compiled but cannot be executed.
That is, the javac is correct. Once java is incorrect, the system prompts that the main class cannot be found or loaded. Baidu Google and its own exploration finally solved the problem.
Summarize the possible solutions to these problems.
1. Check the environment variable path configuration error.
JAVA_HOME is the jdk Path, CLASSPATH is the lib Path (remember to add ..;), and Path is the bin Path.
2. Change the CLASSPATH path.
For example, my original file is.; C: \ Program Files \ Java \ jdk1.8.0 _ 101 \ lib
To.; C: \ Program Files \ Java \ jdk1.8.0 _ 101 \ lib \ dt. jar; C: \ Program Files \ Java \ jdk1.8.0 _ 101 \ lib \ tools. jar
That is, add dt. jar and tools. jar to the original details. (I tried this at the time, but someone said yes .)
3. the java and javac versions are inconsistent.
Enter java-version and javac-version in the cmd window to check whether the version is consistent. if the version is inconsistent, add % JAVA-HOME % \ bin in front of the Path variable.
4. Remove the package name.
That is to say, remove the first line of code package in the. java file.
5. The command entered in the cmd window is incorrect.
In javac, the file name is added to. java. in java, nothing is added to the file name. Example: javac d: HelloWorld. java d: HelloWorld
In addition, files are stored in folders. Of course, each folder should be written (I usually put it in the root directory for convenience ).
I used method 4 to solve the problem. As for other methods, I don't know if it is possible. I just summarized the solutions I encountered at the time. Why?