Java. Lang. noclassdeffounderror Solution

Source: Internet
Author: User

Take the simple and classic "helloworld. Java" as an Example

Helloworld. Java without package Layers

Public class helloworld

     {

     Public static void main (string [] ARGs)

        {

           System. Out. println ("Hello world! ");

        }

    }

Save it in E: \ Java \ SRC and change the current path to: e: \ Java \ SRC under the command line. Compile with the javac command:
E: \ Java \ SRC> javac helloworld. Java

Run:
E: \ Java \ SRC> JAVA helloworld

Screen Printing:
Hello world!

Common Mistakes for beginners
1. The. Class suffix is included in the runtime.
If you try to use the following command:
E: \ Java \ SRC> JAVA helloworld. Class

The system will mistakenly believe that you are running a class file named class under the helloworld package, and will go to the system's classpath (usually including the current directory) to search for helloworld. class. such a class does not exist, and it cannot exist because the class is a keyword and cannot be used as a class name. The following error message is reported:
Exception in thread "Main" Java. Lang. noclassdeffounderror: helloworld/class

2. Incorrect file name case
For systems like Windows, the Case sensitivity is not concerned during compilation. For example, when compiling helloworld. Java, you can also use:

E: \ Java \ SRC> javac helloworld. Java

It can also be compiled, but the generated class file is still consistent with the source file: helloworld. Class.

Note the case sensitivity when running, for example, try to run the following command:

E: \ Java \ SRC> JAVA helloworld
An error similar to 1 will be reported:
Exception in thread "Main" Java. Lang. noclassdeffounderror: helloworld (wrong name: helloworld)

Helloworld. Java that contains the package level
For example, the preceding helloworld. Java modification is as follows:

Package org. javaresearch;

     Public class helloworld

       {

       Public static void main (string [] ARGs)

        {

           System. Out. println ("Hello world! ");

        }

      }

There are two ways to compile

1. Compile directly

E: \ Java \ SRC> javac helloworld. Java

Helloworld. Class is output in the current directory. In this case, you cannot use the same method as above. Use:

E: \ Java \ SRC> JAVA helloworld

The following error occurs during running:

Exception in thread "Main" Java. Lang. noclassdeffounderror: helloworld (wrong name: ORG/javaresearch/helloworld)

You can also see from the preceding error message that the helloworld class can be found in the system (because the current path is included in classpath, why is the wrong name prompted? If you are interested, refer to the Java language standards ), however, this class belongs to Org. javaresearch package. Therefore, you need to create the directory layer based on the above package level and put the generated helloworld. Class in the E: \ Java \ SRC \ org \ javaresearch \ directory. Run ):

E: \ Java \ SRC> JAVA org. javaresearch. helloworld

The system prints:

Hello world!

Note that you cannot run Java org \ javaresearch \ helloworld. the following error occurs:

Exception in thread "Main" Java. Lang. noclassdeffounderrorrg \ javaresearch \ helloworld (wrong name: ORG/javaresearch/helloworld)

Because class names are written using periods to separate the layers of Package Names, classes in the reference Java class library can also be written as follows: Java. util. ListThe full name of the class is like this. If we write import java. util. List in the import statement;

Then, you don't need to write the full name. You can directly write list = new ...; the Java compiler can understand the List class. however, because Java. util package and Java. the SQL package has a date class. Therefore, if both packages are imported

As a result, the Java compiler does not know the class to be referenced. There are two solutions:

A. Use the full name to reference the class: Java. util. Date date1 =...; Java. SQL. Date date2 = ...;

B. Do not import the entire package. Only the required classes are imported. If you need both date classes at the same time, you can only write the full name. If

You do not need these two classes at the same time. You can only import the required packages:

For example: Import java. util. List; import java. SQL. date; in this way, there will be no conflict, because the compiler can clearly differentiate

This date class is in the Java. SQL package, and the date class in the Java. util package is not imported at all.

This can be understood according to common sense.

2. Use the-D <directory> compilation Option

Do you think the above compilation method is a little troublesome? Can you automatically generate a package level under the current path (or any specified path? Yes! Use the-D <directory> compilation option.

E: \ Java \ SRC> javac

(The vertex indicates the current directory. There is a space between D and., and a space between. And the following Java file name)

In this case, an org \ javaresearch directory is generated under the current directory, and the output. Class file is also in it. Run:

E: \ Java \ SRC> JAVA org. javaresearch. helloworld

System print: Hello world!

If you want to store the generated class files in a directory, such as E: \ Java \ Classes, you must first create this directory and then compile it:

E: \ Java \ SRC> javac-d e: \ Java \ Classes helloworld. Java

You can put the generated class file under the E: \ Java \ Classes directory, and create the directory path according to the package level. You can find the helloworld. Class file under E: \ Java \ Classes \ org \ javaresearch. Run the following command correctly (Note that if you want to use other classes, set them in classpath ):

E: \ Java \ Classes> JAVA org. javaresearch. helloworld

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.