Java cannot be found or the main class cannot be loaded

Source: Internet
Author: User

1 Test class When suddenly encountered a very strange problem, the Internet search a lot of information to find a solution, most of the situation is because the class added the package name compiled, execution time did not go to the package execution. It's not the same as the situation I met.

  question: wrote a Test class, and Javac test under CMD. Java can compile and generate the test. class file, but the error occurs when it executes:

  Java Test

1 c:\>java test2 Error: The main class test could not be found or could not be loaded

You can be sure that the environment variable is not a mismatch because there is no problem with executing Java and JAVAC commands directly. Many friends have encountered this problem because the class has been added to the package name. The Java file does not cause the same path as the. class file, and my test class does not have any package names.

Solution:

① can be%classpath by JAVA-CP; Test command to execute with the correct result:

1 C:\>java-cp %classpath; Test2 Hello Servlet

② uses echo%classpath or set Classpath to view the CLASSPATH environment variables and discovers that only the current user's Classpath value is displayed, and the CLASSPATH variable in the system does not have the absolute path of the JDK Lib: .;D: \ program Files\java\jdk1.7.0_80\lib; 。 In retrospect, when the JDK was first installed, it was not configured with Classpath, and after the compilation passed, the corresponding. class file could be executed under the current folder.

Why the error now, and carefully check the system variables found in the configuration of classpath, but there is no jdk Lib path, .;D: \ program Files\java\jdk1.7.0_80\lib; , in order to be able to access the Servlet-api.jar package under Tomcat anywhere in the system, configure the CLASSPATH in the system variables, and come here to understand why the above problem arises.

After installing the JDK, do not configure classpath in the system variable (that is, there is no such variable in the system variable), if the classpath variable is added, it must be configured with the Lib absolute path of the JDK.

It is verified that removing the Classpath,java Test command from the system variable executes successfully, and if you add classpath it is necessary to add the Lib absolute path on the JDK:.;D: \ program Files\java\jdk1.7.0_80\lib; The Java Test command can also be executed successfully.

Java cannot be found or the main class cannot be loaded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.