Take the simple and classic "Helloworld.java" as an example
Helloworld.java with no package level
public class HelloWorld
{
public static void Main (string[] args)
{
System.out.println ("Hello world!");
}
}
Save under E:\JAVA\SRC, command line, change current path to: E:\JAVA\SRC, compile with javac command:
E:\java\src>javac Helloworld.java
Run:
E:\java\src>java HelloWorld
The screen prints out:
Hello world!
Mistakes that beginners often make
1. Runtime with a. Class suffix
If you try to use the following command:
E:\java\src>java Helloworld.class
The system will mistakenly assume that you are running a class file named Class under the HelloWorld package, and will attempt to find the system under CLASSPATH (usually including the current directory). HelloWorld.class.class such a class, such a class does not exist, and it cannot exist because class is a keyword and cannot be the name of a class. Therefore, the following error message will be reported:
Exception in thread "main" Java.lang.noclassdeffounderror:helloworld/class
2. File name capitalization error
For systems like windows, you can compile without worrying about capitalization. For example, when compiling Helloworld.java, you can also use:
E:\java\src>javac Helloworld.java
You can also compile the pass, but the resulting class file is still consistent with the source file: Helloworld.class.
However, be sure to pay attention to capitalization at run time, such as trying to run with the following command:
E:\java\src>java HelloWorld
will report an error similar to 1:
Exception in thread "main" java.lang.NoClassDefFoundError:helloworld (wrong Name:helloworld)
Helloworld.java containing the package hierarchy
For example, the above Helloworld.java is modified as follows:
Package org.javaresearch;
public class HelloWorld
{
public static void Main (string[] args)
{
System.out.println ("Hello world!");
}
}
There are two methods of compiling at compile time
1. Direct compilation
E:\java\src>javac Helloworld.java
At this point, output helloworld.class in the current directory. At this point, the run cannot use the same method as above, using:
E:\java\src>java HelloWorld
, the following error occurred:
Exception in thread "main" JAV A.lang.noclassdeffounderror:helloworld (wrong Name:org/javaresearch/helloworld)
from the above error message you can also see that The system can find the HelloWorld class (because the current path is contained in Classpath, specifically why the wrong name is prompted, interested friends see the Java Language Specification), but this class belongs to the Org.javaresearch package. So, all you have to do is to follow the above package hierarchy, create the directory hierarchy accordingly, and put the helloworld.class generated above into the E:\java\src\org\javaresearch\ directory. Run (at this point in the top-level directory of the package hierarchy):
E:\java\src >java org.javaresearch.HelloWorld
System print out:
Hello world!
It is important to note that you cannot run with Java Org\javaresearch\helloworld, and the following error will also occur:
Exception in thread "main" Java.lang.noclassdeffounderrorrg\javaresearch\helloworld (wrong Name:org/javaresearch/helloworld)
Because the class name is written with a period to split the level of the package name, referencing the Java class Library class can also be written in the:java.util.list class's full name is this, if we write import in the import statement Java.util.List;
So, you don't have to write your full name, just write list list = new ...; The Java compiler can tell what the List is. However, because java.util this package and java.sql have a Date class, if you import both packages at the same time, you will
Causes the Java compiler not to know the reference to that class, there are two workarounds:
A. Reference class with full name: Java.util.Date date1 = ...; Java.sql.Date date2 = ...;
B. Do not import the entire package, only import the required classes, if you need these two Date classes at the same time, you can only write the full name, if
You don't need these two classes at the same time, you can import only the packages you need:
For example: Import java.util.List; Import Java.sql.Date; This will not conflict, because the compiler can clearly distinguish
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 by common sense, or better understood.
2. Use the-D <directory> compile option
Do you think the above compilation method is a bit cumbersome, can you automatically generate the package hierarchy under the current path (or any specified path)? Yes! You can do this with the-D <directory> compile option.
E:\JAVA\SRC >javac
(The point in which the current directory is represented.) D and. There is a space between the. And the Java file name behind it.
At this point, a org\javaresearch directory is generated in the current directory, and the output. class file is also inside. Run:
E:\JAVA\SRC >java Org.javaresearch.HelloWorld
System Printing: Hello world!
If you want to centralize the generated class files in a directory, such as: E:\java\classes, then you first create this directory, and then compile:
E:\JAVA\SRC >javac-d E:\java\classes Helloworld.java
You can put the generated class file in the E:\java\classes directory and create the directory path corresponding to the package hierarchy. You can find the Helloworld.class file under E:\java\classes\org \ Javaresearch. Use the following command to run correctly (note If you want to use a different class, set it in Classpath):
E:\java\classes >java Org.javaresearch.HelloWorld
Solutions for Java Java.lang.NoClassDefFoundError