Easy Tutorial
❮ Cpp Enum Intro Html5 Canvas Clock ❯

Java Compilation and Debugging of Classes with Packages (Creation and Reference)

Category Programming Techniques

Everyone knows how to compile a Java source program, which is simply to navigate to the directory of the source file in the cmd and type javac **.java. But when there is a package declaration in the program, can we still directly javac **.java? The answer is of course no. Let's illustrate this with a simple example to show what happens when we directly javac **.java.

For example: In the directory F:\javaweb2 class\20160531, there is an A.java file, note that there is a package declaration in the source file:

Example

package mypack;
public class A {
    String name;
    int age;
    public void setName(String _name){
        this.name = _name;
    }
    public void setAge(int _age){
        this.age = _age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
    public static void main(String[] args){
        A a = new A();
        //a.setName("zs");
        a.name="zs";
        a.setAge(18);
        System.out.println(a.getName() + a.getAge());
    }
}

Now, let's compile A.java directly with javac A.java. The result is as follows:

We can clearly see that a A.class bytecode file has appeared in the current directory. Can it be run directly? Let's give it a try!

An error occurred, the reason is that we have a package declaration in A.java. When executing the A.java source file, the Java virtual machine first looks for the bytecode file in the current directory of A.java. Although it is found this time, because there is a package declaration in A.java, the Java virtual machine immediately goes to the package directory to see if there is an A.class bytecode file. It can only be executed successfully if it is found. Let's do an experiment! (Here, I will also teach you how to compile A.java with a package declaration, which is to package and compile with javac -d . A.java)

Now the packaging compilation is successful. At this time, we can see that the A.class file has appeared in the mypack directory. Let's try to execute it.

We find that the execution still cannot pass? Why is this? This is a problem that many newcomers encounter. Here, you must remember one thing: the parameter of the Java command is the "full class name" of the class, not the "file name".

The full class name of this source program should be mypack.A, so it should be written like this: java mypack.A

Now it can be executed successfully!

Don't worry!! There's more exciting content below! As the use of packages, how can we not explain the import, creation, and compilation of packages?

This time we introduce another test class Test.java, the code is as follows:

Example

import mypack.A;
public class Test{
    public static void main(String[] args){
        A a = new A();
        a.setName("zs");
        //a.name="zs";
        a.setAge(18);
        System.out.println(a.getName() + a.getAge());
    }
}

The compilation and execution are as follows:

The result is as expected, it can be compiled and executed. The execution process is as follows: After the compilation of Test.java, the generated bytecode file is in the current directory (during compilation, it will look for whether there is an A.class file in mypack. If not, the compilation will not pass). When executed, since there is no package declaration in Test.java, the Java virtual machine first finds the Test.class in the current directory and executes it. When the program references the A class, the Java virtual machine will look for the A.class bytecode file in the current directory. Even if it is found, it will enter the package according to the package import in the source program to find A.class, and it can only be executed successfully if it is found (in fact, it has already been searched during the compilation phase!)

Let's go a step further: If we add a package declaration to the test class Test.java, package mypack1;

Now let's package and compile Test.java. Here I will explain two knowledge points again: 1. When packaging and compiling, the package directory will be automatically created, and you don't need to create a package name folder by yourself; 2. When there are multiple Java files in the current directory that need to be compiled or packaged and compiled, the javac -d . *.java command can compile or

❮ Cpp Enum Intro Html5 Canvas Clock ❯