Execute Java command on cmd-Could not find or Load main class

Source: Internet
Author: User

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

Related Article

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.