Invalid Java engineering structure and classpath & javac commands

Source: Internet
Author: User
Tags java se

As you are too confused about how to use the package, I will summarize my understanding of the usage of the package here: all the files stored in the package, but generally there are three types: 1. Java program source file with the extension. java. 2. Compiled Java class file with the extension. Class. 3. Any other file, also known as resource. Example file, XML file, MP3 file, AVI file, text file ......
1. What is a package?
A package is like a virtual file system that Java uses to organize files. Package organizes the source code. Java file,. Class file, and other files in a organized manner for Java to use. Package organizes files into a Unix-like, Linux File System tree with a root "/" and directories and files starting from the root, the directory also contains files and directories.
2. How does package work?
Once the Source Code declares the package path, it is the package xx. xx. xx starting with the program ". Someone asked, why is it okay to directly put this information in the directory structure? Yes, you can find. Class and. Java in the directory directly. But what if I want to output the package of this. Class? Therefore, we need to leave a package information in. Class.
. Java files are independent compilation units, similar to CPP files in C ++, but they are not required. h file, as long. java is enough. the Java file can contain a public class, several package classes (the package class features no access control modification), and the implicit class, you can also include several protected and private classes. Each class will generate an independent one during compilation. class file, so. java and. the class is not a one-to-one, but a one-to-many relationship. java and public classes are one-to-one. All these. Classes are determined by the package statement starting with. Java in the package.
Package XX. BB. AA; indicates that all classes in this. Java compilation unit are placed in the package XX. BB. AA. The. Java file must be placed in the AA directory under the BB directory under the XX directory. If a. Java file does not have any package statements, all classes in this. Java file are under the package "/", which is also called the default package. It can be seen that the class of the first hello World program written in any Java textbook is in defaultpackage. With the package statement, the situation is more complicated. This compilation unit. Java must be placed under the directory corresponding to the package name. The generated class file must be placed under the corresponding directory structure to operate normally. For example:
/* A. Java */
Package AAA. BBB. CCC;
Public Class {
B = new B ();
}
/* B. Java */
Package AAA. BBB. CCC;
Public Class B {}
During compilation, what is the limit parameter? I will write it in the format of package + file name, javac AAA. BBB. ccc. A. java. Unfortunately, it does not work. A valid path name must be used: javac AAA/BBB/CCC/a. java. However, you find that the generated class has lost the directory structure and appears directly in the current directory. The best way is:
Javac-D bin AAA/BBB/CCC/a. java. In this way, you will see the complete directory structure and properly placed class files under the bin directory of the current directory.
3. What is classpath?

For Java, all required programs and resources must be organized and read in the form of a package. All the things placed in the classpath are the resources that the package receives. Classpath is written as path, but only ZIP files, Jar files, and directories can be written in it. Multiple elements are separated by the current system path separator. The separator number on Linux is ":", and on Windows is ";". Classpath is used by something called classloader in Java. classloader, as its name implies, is used by the load class, but it can also load other things in the package. Currently, classloader in Java has a hierarchical relationship. Generally, we are exposed to classpath environment variables, javac and Java-CP, -The classpath information provided by the classpath parameter is used by appclassloader. Appclassloader is actually the third-level classloader. The highest-level classloader is called Bootstrap.
Classloader is not a Java-written classloader, but written in C ++. The second layer is called extclassloader. The default package includes the classes directory in JRE/lib/EXT and all jar files as the content. The third layer is the command line parameter, or the system environment variable is used to specify the classpath user app classloader without the command line parameter. This is the most basic Java SE.

4. Why is import used?
Import is just a method that allows you to steal a bit of data, so it will never affect your classpath. Remember that there is no reason not to use import, using import won't be similar to embedding a file in C. It is just a convenient way. If it is not a class in classpath, you cannot import it any more. If you do not need import, you need to write Java. util. arraylist if you use the arraylist class. If you use import java. util. arraylist;, you can write arraylist in the code to save trouble. The wildcard * can be used for import. * represents all classes in a package, excluding subdirectories.

========================================================== ========================================================== ====================================

Eclipse can be used for compilation. However, when javac is used in cmd for compilation, the system prompts "not an internal command or an external command...". However, you can still prompt the version when you enter Java-version. In this case, you only need to assign the Java bin path, such as c: \ Program Files (x86) \ java6_x64 \ bin \, to the path environment variable. Restart cmd.

 

Http://www.evget.com/zh-CN/Info/catalog/11033.html ()

Related Article

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.