only a small java compiler should compile the underlying mechanism is not enough

Various compilation of java java file:

As shown A.java Code:

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());
    }
}

As shown Test.java Code:

//package mypack1;  包声明
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());
    }
}

All (A.java, Test.java, mypack folder) are in the desktop directory, this is the background.

1. Compile the file to import the package:

To compile this time but there Test.java import, it is usually the javac Test.java error:

 The first compilation method:

Use javac compiler command cp parameter specifies the file to import the package to tell the compiler that is the position I imported this package where you go to find:

Here to talk about the cp parameter :

cp stands for: classpath role is to compile java files (javac -cp) or byte code file class file (java -cp) tells the compiler where to find the dependent or required class (official called the classpath)

The second method: using the javac command parameter d parameters mean where did write the compiler finished behind -d is the directory to tell the compiler where to put

E.g:

 

 Because compiled compile time so it * .java and compiled A.java package A.java have declared so in mypack also generated mypack then \ mypack \ mypack in nature there A.class

But not so simple:

To do this in order to compile successfully (is not yet quite understand why):

 Here to talk about the change directories:

. "" This is why on behalf of the current directory back above parameters d have a point and a space that is. "."

".." directory on behalf of a "\" (backslash) for the root directory "d:" representative of switching to the D drive remainder empathy.

2. Compile with a package declaration:

A.java is to bring the package declaration, first of all:

But you can not run successfully compiled class files:

 

d javac command parameters with use of the package declaration:

Detailed added:

 

 

Related understand: rookie 

Guess you like

Origin blog.csdn.net/haohao0626/article/details/84867368