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
- Package helloworld;
- public class helloworld {
- public Static void main (string args[])
- {
- int num=10;
- int max=integer.max_value;
- system.out.println ("Hello world!");
- system.out.println ("num++*num++=" + num++*num++ ) ;
- system.out.println ("Max integer=" + max);
-
- }
- }
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