About package, import, Java, and Javac

Source: Internet
Author: User
Tags java se

Package:

Files stored in the package
All the documents, but the general division of the three kinds of
1. Java program source file, extension. java;
2. Compiled Java class file with the extension. class;
3, other documents, also known as resource;
Example slices file, XML file, mp3 file, AVI file, text file ...
What is the package?
The package is like a virtual file system that Java uses to organize files, some of which are similar to folders in the OS.
The package organizes the source code. java files,. class files, and other files in a structured manner for use by Java.
The package is organized into a tree structure similar to the Unix,linux file system,it has a root "/" and then there are directories and files from the root, and files and directories in the directory
How is the package implemented?
The requirements of the source code is the most stringent, and once the source code itself declared in which package path, the class will have their own in which packages below the information, that is the beginning of the program of the "xx.xx.xx". Some people ask, why should have this information, directly put in the directory structure is not good? Yes, you can actually find. class and. Java in the direct directory, but what if I want to output this. class belongs to which package? So we need to leave a package message in the. Class. What if we want to differentiate between classes with the same name A.class? So we need to leave a package message in the. Class.
The. java file is a standalone compilation unit, similar to the CPP file in C + +, but it does not require. h files, as long as. Java is enough, a. java file can contain a public class, several package classes ( The Package class feature does not have any access control adornments, and there are implicit classes that can also contain several protected and private classes.
Each class will generate a separate. class file at compile time, so. Java and. ClassNot one to one, butIt's a one-to-many relationship.But. Java and public classes are one-to。 All of these. Class are determined by the package statement at the beginning of this. Java to determine its location in the package.
Package XX.BB.AA;
Explain this. All classes in the Java compilation Unit are put into the XX.BB.AA package. and corresponding, must put this. java file in the xx directory under the BB directory in the AA directory. If a. java file does not have any package statements, then thisAll classes in Java are under the package's "/", also known as the default package。
You can see that the class of the first Hello World program you generally write from any Java textbook is in the default package.
With the package statement, the situation is a little more complicated. This compilation unit. Java must be placed under the directory corresponding to the package name. The generated class file is also placed under the corresponding directory structure in order to function properly.
For example:
/* A.java */
Package AAA.BBB.CCC;
public class a{
b b=new B ();
}
/* b.java*/
Package AAA.BBB.CCC;
public class b{}

How do I fill in the parameters when compiling? I write according to the format of the package+ file name,
Javac Aaa.bbb.ccc.a.java
Pretty, huh? Pity not to work. Do not use a valid path name:
Javac AAA/bbb/CCC/A.java
But you find that the generated. class and. Java files are in the same directory

Equivalent to executing command javac-d. Aaa/bbb/ccc/a.java
The best way is
  javac-d Bin Aaa/bbb/ccc/a.java
This will see the full directory structure and the appropriate class file in the Bin directory of the current directory.

C:\Documents and Settings\administrator>javac-help
Usage: Javac < options > < source files >
Among the possible options are:
-G generates all debug information
-g:none does not generate any debug information
-g:{lines,vars,source} only generates some debugging information
-nowarn does not generate any warnings
-verbose output messages about the actions that the compiler is performing
-deprecation output using outdated API source location
-classpath < paths > specifying where to find user class files and comment handlers
-CP < paths > specifying where to find user class files and comment handlers
-sourcepath < paths > Specify where to find the input source files
-bootclasspath < path > Overwrite the location of the boot class file
-extdirs < directory > location of the extended directory to overwrite installation
-endorseddirs < directory > location of standard paths for overwriting signatures
-proc:{none,only} controls whether annotation processing and/or compilation is performed.
-processor <class1>[,<class2>,<class3&gt ...] The name of the comment handler to run, bypassing the default search process
-processorpath < paths > specifying where to find comment handlers
-D < directory > Specify the location where the generated class files are stored
-S < directory > Specify the location where the generated source files are stored
-implicit:{none,class} Specifies whether to generate a class file for an implicit reference file
-encoding < encoding > specifying the character encoding used by the source file
-source < version > provides source compatibility with the specified version
-target < version > Generate a class file for a specific VM version
-version Version Information
Summary of-HELP output standard options
-akey[=value] Options passed to the comment handler
-X output a feed of non-standard options
-j< Flag > Direct < flag > pass to runtime system

Compile the class with the jar package:
You can use the command JAVAC-CP *\*.jar Classname.java//*\*.jar represents the path and name of the jar you need, and ClassName represents the class name. It is recommended to use this method.

http://blog.163.com/hatepeng_peng/blog/static/194369112011442333761/

What the package and classpath have to say

For Java, all required programs and resources are organized and read in the form of a package.

So what is Classpath?

Everything that is put into the classpath is the resource that the package is in. Classpath is written as path, but only the zip files, jar files, and directories are generally available. Multiple elements are separated by the current system path delimiter, and the separator symbol on Linux is ":" On Windows ";".
Classpath is used in Java by a thing called ClassLoader, and ClassLoader, as the name implies, is the load class, but it can also load other things in the package. Now Java inside ClassLoader is a hierarchical relationship, generally we often contact the CLASSPATH environment variables, Javac,java-cp,-classpath parameters given by the classpath information is used by Appclassloader .
and Appclassloader is actually the third layer of ClassLoader, the top classloader called Bootstrap ClassLoader, it is not written in Java ClassLoader, but C + + written, The second layer is called Extclassloader, and the default encompass is the classes directory and all jar files inside the Jre/lib/ext as content.
The third layer is our command-line arguments, or without command-line arguments, use the system environment variable to specify the Classpath user app ClassLoader, which is the most basic Java SE.
If it's Java EE, with servers, containers, and more layers of classloader, they're in the bottom of the app ClassLoader, like the jar in the web-inf/lib of a Tomcat Web application, The ZIP and classes directories are used by several layers of ClassLoader under the app.

You can build your own ClassLoader, all under app ClassLoader, and in fact Tomcat itself builds classloader.
The purpose of layering is to be safe, just imagine you join a classloader, read class from the network, and write the code in the format hard disk, people read run, that does not hang, so after layering, first from the highest level of reading, no further down to find it.
Some of the classes that Java must have in Rt.jar are read in when Bootstrap ClassLoader is started, and several jars used by JMF are read in ext ClassLoader.
In other words, when we read these classes, our appclassloader is still in womb, so it is completely foolish to specify Rt.jar in class path. Java will never come here to find Rt.jar, and bootstrapclassloader if you do not specifically want to modify, it is a constant, do not need your care.

Import

Import is just a way for you to steal a little bit lazy typing, will not affect your classpath, this you have to remember, there is no non-use import reasons, with the import will not be similar to the C embedded in a file content effect, it is only a convenient way.
Not in the Classpath class, let you import also useless.
If you do not import, you use ArrayList this class, you need to write

Java.util.ArrayList.
and using the import java.util.ArrayList;
Later in the code to write ArrayList on it, easy.
Import can use the wildcard character *,* to represent allclasses under a package, excluding subdirectories .
Import java.awt.*
Not equal to
Import java.awt.*
Import java.awt.event.*
If you want to abbreviated java.awt.event and java.awt under the class, you can not lazy, two are to import.

Http://blog.chinaunix.net/uid-22028680-id-3042341.html

Introduction to package, import, Java, and Javac (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.