In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the principle of java's package and import mechanism? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Before talking about the mechanism of package and import, let's take a look at the CLASSPATH of java.
CLASSPATH, as its name implies, is the path to class. When we run a java program on the system, it tells the system to look for the class file in these places.
CLASSPATH=.;%JAVA_HOME%\ lib;%JAVA_HOME%\ lib\ tools.jar; this is our java default environment variable setting.
"." It means to look in the current folder;% JAVA_HOME%\ lib is found in the lib folder of the java installation path; and% JAVA_HOME%\ lib\ tools.jar is found in the tools package in the lib folder of the java installation path.
Now let's test it:
Create a new HelloWorld.java in C:\ DOM
/: HelloWorld.java public class HelloWorld {public static void main (String [] args) {System.out.println ("HelloWorld!");}}
First javac HelloWorld.java, the system will find the HelloWorld.java file in this directory and compile the HelloWorld.class file. Java HelloWorld, the system will first call the CLASSPATH path, we set the "." This directory, so you will find HelloWorld.class in the current directory. It runs successfully.
* * run this program in another place
We run java HelloWorld** on disk C.
The system will now look for HelloWorld.class in the CLASSPATH path. But in "." (now on disk C) the file could not be found. We will now add the C:\ DOM path to CLASSPATH
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 the file HelloWorld.class. It runs successfully. Now we understand the function 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!");}}
Compiled successfully, but could not be run.
When the system finds HelloWorld.class, it reads package a.b; this information tells the system that the HelloWorld.class is to be placed in the b folder of folder a. But now our HelloWorld.class is in the DOM folder. The file path does not correspond to package and cannot be run.
Now let's create two new folders called HelloWorld.class b, and then copy it to the b folder. Then run java HelloWorld
Sorry! It still didn't run successfully. The system looks for the HelloWorld.class file in CLASSPATH, but this file is hidden in the b folder in folder a. Let's try java a.b.HelloWorld.
Run successfully, the system in CLASSPATH "." Folder find folder a, then folder b, and find the HelloWorld.class file. Now that you understand the role 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 program that calls a\ b\ HelloWorld.class in the C:\ DOM directory.
/: TestHelloWorld.javaimport a.b.HelloWorlds public class TestHelloWorld {public static void main (String [] args) {HelloWorld hw=new HelloWorld (); hw.main (args); / / call the main () function in HelloWorld. Args is an argument and will print out HelloWorld!}}
Compile and run successfully!
Compilation: the system finds the TestHelloWorld.java file in this folder and checks import a.b.HelloWorld.For CLASSPATH, find a\ b\ HelloWorld.class. Compilation completed!
Run: the system now finds TestHelloWorld.class in CLASSPATH, then reads import a.b.HelloWorld; imports HelloWorld.class, and runs successfully!
This is the answer to the question about what is the principle of package and import mechanism of java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.