Build and use their own library

Note: This only using a text editor and console

In order to make their library has a unique name, you can learn the practice of Java class libraries, the use of reverse domain name as the global name of the class, such as my domain is baixo.cn, the cn.baixo is the class that I created unique global name. If I want to create a library named test, you can get the name of a package:

Package Penalty for cn.baixo.test;   // Package Penalty for keyword is used to generate the library contains a set of class files 

Now create your own classes in this package, the file name MyPackageTest.java:

package cn.baixo.test;

public class MyPackageTest{

    public MyPackageTest(){

       System.out.println("cn.baixo.test.MyPackageTest");

    }

}

MyPackageTest.java file in the subdirectory on my system:

E: \ files_info \ custom_library \ JavaTL \ cn \ baixo \ test, where the package name decompose to a directory, all the .class files in this library directory (this example is compiled to get the MyPackageTest. class files).

After built his library, if you want to use this library, you first need to configure the environment variable CLASSPATH, java interpreter will find it to be used as a .class file in the root directory. On my system CLASSPATH variable value:

.;% JAVA_HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ tools.jar; E: \ files_info \ custom_library \ JavaTL; // in front of the root of the Java class libraries, followed by my own library root directory

You can now use test libraries, the following java files can be placed in any directory, such as D: \ Java directory:

import cn.baixo.test.*;

public class Test{

    public static void main(String[] args){

        MyPackageTest test0 = new MyPackageTest();  

    }

}

D:\Java>javac Test.java

D:\Java>java Test

Output: cn.baixo.test.MyPackageTest

When the compiler encounters the import statement, they begin to look at the directory specified in CLASSPATH subdirectory cn \ baixo \ test, and then locate the .class files with matching names from the directory, in terms of MyPackageTest is MyPackageTest.class.

 

Next, if you wish, you can gradually collect the class of their own.

Guess you like

Origin www.cnblogs.com/shiyiaccn/p/11795512.html