Analysis of package and import mechanism of Java

Time:2020-5-20

This article mainly introduces the principle analysis of java package and import mechanism. The example code is introduced in detail in this article, which has a certain reference value for your study or work. You can refer to the following for your friends

Before we talk about the package and import mechanism, let’s first understand the Java classpath.

Classpath, as the name implies, is the path of class. When we run a java program in the system, it will tell the system to find the class file in these places

CLASSPATH=.;%JAVA_ HOME%\lib;%JAVA_ HOME%\lib\ tools.jar ; this is our Java default environment variable setting.

“.” means to find in the current folder;% Java_ Home% \ lib is found in the Lib folder of the Java installation path;% Java_ HOME%\lib\ tools.jar It is found in the tools package of the Lib folder of the Java installation path.

Now let’s test it:

New in C: \ DOM HelloWorld.java


//: HelloWorld.java
 public class HelloWorld{
  public static void main(String[] args){
    System.out.println(“Hello World!”);
  }
}

First javac HelloWorld.java , the system will find it in this directory HelloWorld.java Files, compiling out HelloWorld.class Documents. Java HelloWorld, the system will call the classpath first. We set the directory “.”, so we will find it in the current directory HelloWorld.class 。 Run successfully.

**Run the program somewhere else

We run Java HelloWorld on Disk C**

The system will now find the classpath HelloWorld.class 。 But this file can’t be found in “.” (now C disk).
Let’s now add the C: \ DOM path to the classpath

In order to update the system environment variables, we need to restart the DOS window

The system will find the path C: \ DOM in classpath, and then find HelloWorld.class This file. Run successfully.
Now we understand the role of classpath^^

About package

Let’s rewrite this program and add package


//: HelloWorld.java
 package a.b;
public class HelloWorld{
  public static void main(String[] args){
    System.out.println(“Hello Wolrd!”);
  }
}

Compilation succeeded, but could not be run.

System is finding HelloWorld.class It will read package A.B; this information tells the system this HelloWorld.class Is to be placed in folder a’s folder B. But now our HelloWorld.class It’s in the DOM folder. The file path and package cannot correspond and cannot be run.

Now let’s create two folders A / B, and then HelloWorld.class Copy to folder B. Then run Java HelloWorld

I’m sorry! Still not running successfully. System searching in classpath HelloWorld.class File, but this file is hidden in folder a in folder B.
Let’s try Java a.b.helloworld

The operation is successful. The system finds folder a in the “.” folder in the classpath, then Folder B, and finds HelloWorld.class Documents.
Now that you understand the function of package, it must conform to the directory structure starting from classpath.

About import

The function of import is to tell the system where to find the class when you call another class.

We write a call to a \ B in the directory of C: \ dom\ HelloWorld.class The procedure of.

//: TestHelloWorld.java
import a.b.HelloWorld;
public class TestHelloWorld{
  public static void main(String[] args){
    HelloWorld hw=new HelloWorld();
    hw.main (args); // call the main() function in HelloWorld, args is the argument, and HelloWorld will be printed!
  }
}

Compile and run successfully!

Build: found in this folder TestHelloWorld.java File, check import a.b.helloworld; find a \ B in classpath\ HelloWorld.class 。 Compilation completed!

Run: system now found in classpath TestHelloWorld.class , and then read import a.b.helloworld; import HelloWorld.class , run successfully!

So far, you should understand the function of package and import.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

IView, upload component beforeupload compress image

critical code <Upload multiple :format=”[‘jpg’,’jpeg’,’png’]” :beforeUpload=”beforeUpload” action=”” :on-success=”upload” > < button icon = “IOS cloud upload outline” > upload picture < / button > </Upload> import Compressor from “compressorjs” beforeUpload(file) { return new Promise((resolve, reject) => { let isLt1M = file.size / 1024 / 1024 < 1; if (isLt1M) { resolve(file); } new Compressor(file, { […]