With the successful installation of JDK and Eclipse, few development environment issues are encountered in the development of test Java projects.
This is thanks to Eclise's management through Engineering (project) and packages.
Occasionally, however, when compiling and running Java files at the command line, you will encounter the following error:
(The following examples are all performed on Win7)
d:\temp> Java TMP Sometimes this error can also be caused by improper package setup in the Java source program.
1. There is no package declaration statement at the beginning of the Java source file.
D:\temp> Javac Tmp.java
d:\temp> Java TMP
Hello world!
2. There is a package declaration statement at the beginning of the Java source file packages com.hdz.test;
D:\temp> Javac Tmp1.java
d:\jtest> Java TMP1
Error:could not find or Load main class TMP1
Cause of Error:
The format for compiling Java source Program files containing package declaration statements is:
javac-d Your_path Your_class.java
This command can generate Your_class.class under your_path/your_package/.
The above command does not have the-D option, and Javac generates J_hello1.class (no error) in the current directory.
However, executing the Java command will cause an error if the class is not found.
Workaround:
With javac-d. Tmp1.java compile, the./your_package_path/tmp1.class will be generated directly.
D:\temp> javac-d. Tmp1.java
d:\temp> Java COM.HDZ.TEST.TMP1
Hello world!
Note: It is not valid to proceed directly to the path where the package is located:
D:\temp> cd/d D:\temp\com\hdz\test
d:\temp\com\hdz\test> Java TMP1
Error:could not find or Load main class TMP1
The following are the instance programs:
// D:\temp\Tmp.java public Tmp { public static void main (string[] args) {System.out.println ( "Hello world!" ); }}
// D:\temp\Tmp1.java Package com.hdz.test; Public class TMP1 { publicstaticvoid main (string[] args) { System.out.println ("Hello world!" ); }}
Execute Java command on cmd-Could not find or Load main class