Java Package
The Java package is a mechanism for organizing various Java classes, interfaces, enumerations, and annotations into namespace. The package effectively solves the problem of duplicate type names, the same package type can be accessed from each other, and different package types are accessed either by using Import statements or by using a fully qualified name reference that contains namespace.
The package essentially corresponds to the directory structure of the file system, and when the program is written, it uses the packages AA. Bb. CC statement, after compiling, in the working directory should have the Aa/bb/cc/xx.class file structure corresponding to it. When you do not use the package statement, it means that you use the default, local working directory, which generates the Xx.class file in the working directory after compilation.
Most of the time we use IDE development, such as Eclipse, to automatically invoke the appropriate Javac command to compile, but what if we call it ourselves manually?
If your code uses the package statement, be sure to call the appropriate JAVAC command to generate a reasonable bytecode file directory.
D:\java>javac-d. -verbose helloworld.java[parsing start time regularfileobject[helloworld.java]][parsing completed, spents 30 milliseconds [source file search path:.] ...
... D:\java>
Compilation is completed and the directory structure corresponding to the package is generated in the local directory
Invoking with the Java command
D:\java>java Cn.leo.demo.HelloWorldHello
Java Classpath
Classpath is a configuration parameter when the Java Virtual machine (Java command), the Java compiler (Javac command) works, to specify the user-defined type and the location of the package. Classpath can be temporarily set at the command line, or it can be configured to remain active for the long term in system environment variables.
Continue to see the above example, because there is no setting class_path environment variable, in fact, Java will be in the local directory to find Helloworld.class file, Cn.leo.demo.HelloWorld represents d:/java/cn/leo/demo/ Helloworld.class.
If the class to be referenced is not in the current directory, be sure to use the-CP or-classpath option to specify the class search path
D:\java\cn>java Cn.leo.demo.HelloWorld
Error: The main class could not be found or could not be loaded Cn.leo.demo.HelloWorld
D:\JAVA\CN>JAVA-CP. /cn.leo.demo.helloworldhellod:\java\cn>
If the class to be executed references a type that is not found by the current Classpath search path, modify the current classpath to add a path that can be searched
For example
Helloworld.javapackage Cn.leo.demo;import Com.leo.demo.hello;public class helloworld{public static void main ( String[] args) { Hello Leo = new Hello (); Leo.say (); }} Hello.javapackage Com.leo.demo;public class helloleo{public void Say () { System.out.println ("Leo");} }
The HelloWorld class is in the Cn.leo.demo package, and the Helloleo class is in the Com.leo.demo package, and the HelloWorld class references the Helloleo class.
First compile the Helloleo class, then compile the HelloWorld class to ensure that the dependency resolution is correct
D:\java>javac-d. Hello.javad:\java>javac-d. Helloworld.javad:\java>java CN.LEO.DEMO.HELLOWORLDLEOD:\JAVA>CD. D:\>JAVA-CP Java Cn.leo.demo.HelloWorldLeo
Java Foundation--classpath and package