Java command line running Java program, "Cannot find or cannot load main class" Considerations

Source: Internet
Author: User

Reference: http://blog.chinaunix.net/uid-27106528-id-5209914.html

To run a Java program using Java on the cmd command line, a workaround for "The main class cannot be found or cannot be loaded" error,
Basically on the network is said about the "classpath" path configuration problem, to join ".; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; "
Before adding ".;", the surface should look for classes under the current directory.
If you have tried many times, Classpath also configured the correct, still appear this error, note that you use the test code, whether under a certain package name;
Example: The code is as follows:

Click (here) to collapse or open

    1. Package helloworld;
    2. public class helloworld {
    3.     public  Static void main (string args[])
    4.     {
    5.          int num=10;
    6.         int max=integer.max_value;
    7.         system.out.println ("Hello world!");
    8.         system.out.println ("num++*num++=" + num++*num++ ) ;
    9.         system.out.println ("Max integer=" + max);
    10.               
    11.     }
    12. }

In the current directory where the file is located, run Javac Helloworld.java compile successfully, generate Helloworld.class file, this time, you use Java HelloWorld want to run the program, there will always be "can't find or load the main class" error.
This time to note that the reason is not found, because HelloWorld in the HelloWorld package name below.
Remove "package HelloWorld;" Re-compile Helloworld.java with Javac and run Java HelloWorld again.
or create a new folder with the same package name, in this case, to create a HelloWorld folder, move the Java file to that directory.
Then, under the HelloWorld folder, run the javac Helloworld/helloworld.java compiler and run Java Helloworld.helloworld to run the Java program that contains the package name.

Principle Description:
Java program runs the class file, for the class with the package name, Java treats the package name as a folder, "package name + class name" is equivalent to "folder Directory + class name" to find the class.

Java command line running Java program, "Cannot find or cannot load main class" Considerations

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.