"Summary" Java command parsing and compiler, how virtual machines locate classes

Source: Internet
Author: User

Learn Java for some days, always use the IDE to write programs. The advantage is that I can even compile with the command line, explaining that the Java source code is not known, it is not clear how the compiler and virtual machine (including bytecode interpreter) in the JDK are positioned to the class file. Sorrow ....

1, install the JDK, configure environment variables.

The directory where the JDK resides is not configured in the system environment variable, how can the system find the compiler in the JDK, where is the interpreter? If you do not specify the location of the Bin folder for the JDK, the Javac/java command cannot be found in the shell. That's not much to say.

2, compile, explain the implementation of Java program. "Javac Command/java command"

(1) Test.java Source code

<span style= "Font-size:small;" >// default package, where the source of the program is located: E:/project/test.javapublicclass  test{.....        Public Static void Main (string[] args) {      ...       </span>

Compile command: "Javac E:/project/test.java" generated Test.class in E:/project directory

Note: ① If you want to generate Test.class in the specified directory, you can use the javac-d command, such as "javac-d c:/ E:/project/test.java " generate T Est.class (i.e. E:/test.class) under the c:/directory

It is not misunderstood that-CP in ②JAVAC-CP is not a directory that specifies Test.java. -cp/-classpath can only be the path to the specified class file (. class file). The above command cannot be written as: JAVA-CP E:/project Test.java

Interpreting execution Commands : " J AVA-CP E:/project Test " will invoke the interpreter to execute the Test.class bytecode in E:/project.

Note: ①-CP is the location of the specified user class file, such as the location of the test.class above. This is because you want to find the Test.class class file instead of Test.java the source code file, so you specify it by-CP. There is no such execution command: Java e:/project/test

(2) Test.java source code

 <span style= "Font-size:small;" >//  default package, but the source code references a custom class within a jar package located in the c:/  Directory  import  Net.single.util.SL; //  public  class   test{ private  SL aobject=new  SL (); //        Initialize the SL class in the Jar  public  static  void   main (string[] args) {...}}  </span> 

Compile command: "JAVAC-CP C:/single.jar E:/project/test.java" generated Test.class in the E:/project directory

Note: If you are currently compiling a Java file that references other classes, the. class file for that reference class is not in the current directory (or in another directory, or within the. Zip/.jar), which is required after the Javac command, plus-cp/- The classpath parameter to indicate the location of these classes. There are generally three ways of specifying:

① absolute or relative path: JAVAC-CP c:/single.jar Test. java or JAVAC-CP. /single.jar Test. Java (where.. Indicates the previous level of the directory)
② system variable: JAVAC-CP%classpath% Test. Java (where:%classpath% represents a lookup using the value of the system variable CLASSPATH, assuming that the Single.jar path is included in the CLASSPATH system variable)

③ current directory: JAVAC-CP./single.jar Test.java (where. Represents the current directory)

Interpreting execution Commands : "Java-cp c:/single.jar;e:/project Test"

Note: The path to ①-CP not only specifies the location of the desired Single.jar, but also the location of the compiled Test.class.

Different items in the ② classpath are delimited by delimiters, the UNIX system delimiter is a colon (:), and Windows is a semicolon (;)

(3) Test.java source code

<span style= "Font-size:small;" >// This class in the Net.single package, the class does not introduce a custom class in the other directory   net.single;  Public class test{.....         Public Static void Main (string[] args) {       ...       </span>

compile command: "Javac -D. E:/project/test.java "

Note: ① compiles javac E:/project/test.java without the-D. A test.class will be generated directly under the E:/project directory, but this test.class cannot interpret execution because it is actually in the Edu.single package. Therefore, the package must be compiled together, where the-D parameter is used.

② the above compilation results will automatically create the file directory under the e:/directory based on the structure of the package E:/net/single/test.class

Interpreting execution Commands : "Java -CP e:/net.single.Test "

now let's summarize:

[A.] It is difficult to compile a large project without an IDE environment, because classes that need to be referenced by other classes must be compiled first, and it is better to compile the package structure together. So the General command format is as follows:

Compile: JAVAC-CP (required class file path 1; class file path 2; ...)-D (the location directory where the compiled class file is stored) (file path to be compiled)

execution: JAVA-CP (need to interpret the class file path for execution) (class file with package)

Example: Now to compile a class source: Test.java, where the class is located under e:/project/

(1. The test source code uses a class from a jar package that is located in the c:/directory.)

(2. The test source code uses a custom class content, this class of source Content.java is located under e:/

(3. Test package is Net.single,content package is Net.single.cont

Workaround: Step 1: Because Test uses the content class, the content must be compiled first, and the content class is in the e:/directory and belongs to the package Net.single.cont

Compile command: javac-d. E:/content.java

Compilation result: a Net/single/cont/content.class file (with package structure) is generated in the current directory of Content.java, i.e. E:/net/single/cont/content.class

Step 2: Compile the test class and indicate the location of the introduced Single.jar package and Content.class

Compile command: JAVAC-CP c:/single.jar;e:/net/single/cont-d. E:/project/test.java

Compilation result: A Net/single/test.class file is generated in the previous level of Test directory, which is e:/Net/single/test.class

Step 3: Explain the execution test.class

Execute command: JAVA-CP c:/single.jar;e:/net.single.Test

3, compiler, how to locate the virtual machine to the class

 Package Net.single; import java.util.*; import net.single.util.*;  Public class test{     //Singleutil class in C:/single.jar under the Net.single.util package     Private singleutil sut=new  singleutil ();}

Compile command: JAVAC-CP c:/single.jar-d. E:/project/test.java

The compiler first finds E:/project/test.java. The test source code is then compiled, and when compiled into the statement that creates the Singleutil class object, the compiler begins to look for the location of the Singleutil.class. The compiler first finds the location of all the packages that contain the class, and queries all the import directives to determine if the referenced class is included.

As Test.java above, the compiler will attempt to find Java.lang.singleutil,java.util.singleutil,net.single.util.singleutil and SingleUtil in the current package ( i.e. Net.single.SingleUtil). The compiler will look for the class file in three parts:

(1) Find the Java.lang,java.util and Net.single.util packages in the standard class library file in the JDK's Lib directory. Obviously only Java.lang and java.util bags can be found. Then find the Singleutil class file in both packages. Of course I can't find it.

(2) Find the Java.lang,java.util and Net.single.util packages under the Classpath (C:/single.jar) indicated by the-CP parameter in the compile command. Obviously you can only find the Net.single.util package, and then find the Singleutil class file inside.

(3) in the current directory of Test.java to find Singleutil, is also not.

If Singleutil is not found, or multiple singleutil are found. Compiler error.

Summary Java command parsing and compiler, how the virtual machine locates classes

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.