Java notes--java and Javac

Source: Internet
Author: User

Read Catalogue
    • Java JDK Installation
    • PATH
    • CLASSPATH
    • Summary
    • Javac command
    • Java commands
    • Summary
    • Build Jar Package
One, Java JDK installation

1) The JDK will be installed two times during installation, the 1th time is to install the JDK, and the 2nd time to install the JRE. I installed the JDK under the D:\Java\jdk\ path and installed the JRE under the D:\Java\jre\ path.

2) When configuring the values for Paht and CLASSPATH, is the configuration of the JDK installation path regardless of the JRE installation path (which is, of course, only in general)


[Back to top]


Two, PATH

1)path To configure is:;D: \java\jdk\bin;d:\java\jdk\jre\bin (two paths are configured here, the semicolon is the delimiter for each path). Usually we will add a variable java_home, whose value is configured as: D:\JAVA\JDK (note that there is no semicolon at the beginning or end of the semicolon), so the value of PATH is usually configured as:;%java_home%\bin;%java_home%\jre\bin

2)benefits of adding Java_home variables:When I reinstall the JDK, if I want to change the JDK installation path to E:\Java\jdk\, when I do not configure java_home, I have to change the value of the path one at a time, for example, instead; E:\Java\jdk\bin; E:\Java\jre\bin, but if I have the value of configuring Java_home, I just need to change the value of Java_home to E:\JAVA\JDK, and the value of PATH can remain the same.

3)Configure the role of the PATH environment variable:When we compile the Java program with the command line, we often use the Java and Javac commands, when we have not set the value of the path, the two commands can not be executed in the command line arbitrary file directory, then the configuration path value is to let these two commands to execute

4)How the Java and Javac commands work:We can see a bin folder under the JDK installation path, and in the Bin folder you will find Java and Javac here. Java and Javac are both. exe programs, and on the command line, if you want to execute an. exe program, you have to go from the command line to the directory where the. exe program is placed, and we can enter the CD/D D:\Java\jdk\bin command on the command line, then the command lines enter B In file directory, you can execute the Java and Javac commands even if we do not configure the value of PATH, and you can enter the java-version command at the command line to try it. But what if we want to execute Java and JAVAC commands in any directory on the command line? Obviously, you want to configure the value of PATH. When we execute the Java command, the system will first look for a java.exe program in the current directory, there, then execute, no, then go to the path inside the search, so, when we configured the path, in any directory execution Java and Javac will take effect


[Back to top]


Third, CLASSPATH

1)value of CLASSPATH:.; %java_home%\lib;%java_home%\lib\tools.jar (3 paths are configured here.) The symbol indicates a local path, and the other two paths can be found under the JDK's installation path)

2)Configure the role of CLASSPATH:Look for the. class file. For example: If I have a Java program under the E:\Example\ path, the file name is called Test.java. To compile the program, first switch the command line to the E:\Example\ directory (enter command cd/d E:\Example\ at the command line), and then execute the javac test.java command to compile the. java file into a. class file. When CLASSPATH is not configured: We can execute the Java Test command directly in the command line's E:\Example\ directory, and the reason for the success is that the system will find the compiled Test.class file by default in the local directory when no CLASSPATH is configured. When there is configuration CLASSPATH: If I randomly add a path to the CLASSPATH f:\; , then the system will not be found by default in the local directory, we also want to execute the Java Test command in the command line E:\Example\ directory, the CLASSPATH should be configured as.; F:\; (. Symbol indicates a local directory)

3)Let me give you an example:My CLASSPATH has been configured to:.; %java_home%\lib;%java_home%\lib\tools.jar, now I put the compiled Test.class file under the D:\ExampleClass\ path, then I want to execute JAVA Test on the command line What about the order? There are two methods, the first way: I switch the command line to the D:\ExampleClass\ directory, and then I can execute the Java Test command directly (because CLASSPATH is configured. To find the Test.class file in a local directory). The second method: Add the D:\ExampleClass\ path to the CLASSPATH, that is.; %java_home%\lib;%java_home%\lib\tools.jar;d:\exampleclass, then we can execute JAVA Test in any directory on the command line


[Back to top]


Iv. Summary

1) PATH is configured to enable Java and Javac commands to execute in any directory on the command line

2) Configure CLASSPATH to be able to find the Test.class file when executing the Java Test command

3) The configuration of path and CLASSPATH is related to the installation path of the JDK, regardless of the installation path of the JRE (which is, of course, only in general)

4) Add the specified path to CLASSPATH to enable Java Test to execute in any directory on the command line, and use an absolute path to make Javac E:\Example\Test.java in any directory on the command line


[Back to top]


V. JAVAC command
    1. Format: javac [options] [Source files]
    2. Common options are:
      • -D < directory >, which is used to store. Class and. java files separately during compilation
      • -sourcepath < path >, which is used at compile time to specify the location of the. java file
      • -classpath < path >, which is used at compile time to specify the location of the. class file
    3. Usage

No options

Suppose there is a file named Test.java under the D:\sources\ path.

For example: Javac D:\sources\Test.java, you can enter the command in any directory on the command line, compiled, the system will generate Test.class files by default under D:\sources\ path

- D < directory >(before you use this option, create a directory on your computer)

Suppose there is a file named Test.java under the D:\sources\ path.

For example: javac-d E:\myclasses D:\sources\Test.java, you can enter the command in any directory on the command line, after compilation, the system will generate Test.class files under the E:\myclasses\ path (note: You must first Establish the E:\myclasses\ path in the computer)

-sourcepath < paths >

Example 1 (. java file under the same package):

Assuming that the file is located under the E:\com\example\ path, the file name is Fruits.javapackage com.example;public class fruits{@Overridepublic String toString ( {return "Fruits";}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage com.example;public class test{public static void Main (string[] args) {Sys Tem.out.println (New Fruits ()); Reference Fruits Object}}

Program:

    • Two. java files are located under the same package (because all packages com.example)
    • Test references the Fruits object

How to compile the Test.java file: (to establish the D:\classes\ path in the computer first)

    • The first method: Switch the command line to the D:\sources\ directory, and then execute Javac-sourcepath e:\-D D:\classes Test.java
    • Second method: Under any directory in the command line, execute the Javac-sourcepath e:\-d D:\classes D:\sources\Test.java command
    • The third method: add to the computer environment variable CLASSPATH; E:\ Then execute the javac-d D:\classes in any directory on the command line D:\sources\Test.java

Run Test.class:

    • Switch the command line to the D:\classes\ directory and execute the Java com.example.Test

Example 2 (. java file under a different package):

Assuming that the file is located under the E:\hzl\net\example\ path, the file name is Person.javapackage net.example;public class person{@Overridepublic String ToString () {return ' person ';}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage com.example;import Net.example.Person; To import the package public class Test{public static void Main (string[] args) {System.out.println (New person ());//reference Person object}}

Program:

    • Two. java files are located in different packages (package net.example; and package com.example;)
    • Test refers to the person object

How to compile the Test.java file: (first to establish the D:\yourclasses\ path in the computer)

    • The first method: Switch the command line to the D:\sources\ directory, and then execute the javac-sourcepath E:\hzl-d D:\yourclasses test.java Command (note: if I Person.java The package name is changed to packages hzl.net.example; Then the import of Test.java should be changed to import Hzl.net.example.Person; The compilation command to execute should be Javac-sourcepath e:\-D D:\yourclasses Test.java)
    • The second method: Execute Javac-sourcepath E:\hzl-d D:\yourclasses in any directory on the command line D:\sources\Test.java
    • The third method: add to the computer environment variable CLASSPATH; E:\hzl then executes javac-d D:\yourclasses in any directory on the command line D:\sources\Test.java

Run Test.class:

    • Switch the command line to the D:\yourclasses\ directory and execute the Java com.example.Test

Example 3 (Nature of-sourcepath):

When you execute a command that contains this option, the system will temporarily overwrite the path in the CLASSPATH with the specified < path >

Assuming that the file is located under the E:\net\example\ path, the file name is Man.javapackage net.example;public class man{@Overridepublic String toString () { return "Man";}}
Assuming that the file is located under the F:\cn\example\ path, the file name is Woman.javapackage cn.example;public class woman{@Overridepublic String toString () { return "Woman";}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage com.example;import net.example.man;import cn.example.woman;public Class Test{public static void Main (string[] args) {System.out.println (New Man ());//Reference Man object System.out.println (new Woman ()); Reference Woman Object}}

Program:

    • There are three. java files in different packages
    • Test references the man and Woman objects

1. When CLASSPATH is not configured:

Compile: (first to establish the D:\myclasses\ path in the computer)

    • Execute Javac-sourcepath e:\ under any directory on the command line; F:\-D D:\myclasses D:\sources\Test.java

Run:

    • Switch the command line to the D:\myclasses\ directory and execute the Java com.example.Test

2, the value of CLASSPATH is configured as.; E:\ (. symbol indicates local directory, e:\ is the path where the Man.java file is stored):

Compile:

    • Execute Javac-sourcepath f:\-D D:\myclasses D:\sources\Test.java in any directory on the command line (compilation fails, although the Man.java path has been configured to CLASSPATH , but the program still cannot find Man.java because the path temporarily overrides the value in CLASSPATH when you specify-sourcepath < path >)

3, want to let the compilation through:

    • The first method: E:\ and f:\ Are configured in CLASSPATH, that is, the value of CLASSPATH is.; E:\; F:\, and then execute javac-d D:\myclasses in any directory on the command line D:\sources\Test.java
    • The second method: regardless of the value of the CLASSPATH, execute Javac-sourcepath e:\ directly under any directory in the command line; F:\-D D:\myclasses D:\sources\Test.java

-classpath < paths >

Similarly, when you execute a command that contains this option, the system will temporarily overwrite the path in the CLASSPATH with the specified < path >

-sourcepath can achieve the function,-classpath can also be achieved, but-classpath can achieve the function,-sourcepath may not be able to achieve, for example:

Most of the time, we only have a. class file or a jar package that consists of a. class file, so we can compile the program without using-sourcepath instead of using-classpath to specify the path so that the program compiles through

Assuming that the file is located under the D:\cn\example\ path, the file name animals.java//assumes that I have compiled it into a animals.class file and placed it under the F:\cn\example\ path of the package Cn.example;public class animals{@Overridepublic String toString () {return "Animals";}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage com.example;import cn.example.Animals; Import package public class Test{public static void Main (string[] args) {System.out.println (New Animals ());//reference Animals object}}

Program:

    • If I only have Animals.class file now, there is no Animals.java file (animals.class file is located under F:\cn\example\ path)
    • The Test class references an Apple object

How to compile the Test.java file: (first to establish the D:\myclasses\ path in the computer)

    • Run Javac-classpath f:\-D D:\myclasses D:\sources\Test.java (where-classpath specifies the path to apple.class) in any directory on the command line

Run Test.class:

    • Execute Java-classpath D:\myclasses under any directory on the command line; E:\ com.example.Test

[Back to top]


Vi. Java commands
    1. Format: Java [options] [Class files]
    2. Common options are:
      • -classpath < paths >
    3. Usage

-classpath < paths >

Example 1:

Assuming that the file is located under the D:\cn\example\ path, the file name animals.java//assumes that I have compiled it into a animals.class file and placed it under the F:\cn\example\ path of the package Cn.example;public class animals{@Overridepublic String toString () {return "Animals";}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage com.example;import cn.example.Animals; Import package public class Test{public static void Main (string[] args) {System.out.println (New Animals ());//reference Animals object}}

Compile:

    • Execute Javac-classpath f:\-D D:\myclasses D:\sources\Test.java in any directory on the command line

Run:

    • Execute Java-classpath D:\myclasses under any directory on the command line; F:\ Com.example.Test

Example 2:

When you execute a command that contains this option, the system also temporarily overwrites the path in the CLASSPATH with the specified < path >

Assuming that the file is located under the D:\sources path, the file name is Test.javapackage com.example;public class test{public static void Main (string[] args) {Syst Em.out.println ("Hello world!");}}

1, when the value of CLASSPATH is not configured:

Compile:

    • Execute javac-d D:\myclasses under any directory on the command line D:\sources\Test.java

Run:

    • Switch the command line to the D:\myclasses\ directory, and then execute the Java-classpath. Com.example.Test (< path > for in-classpath. Symbol, indicating that a local directory is specified)

2, the value of CLASSPATH is configured as. Symbol

Compile:

    • Execute javac-d D:\myclasses under any directory on the command line D:\sources\Test.java

Run:

    • Switch the command line to the D:\myclasses\ directory, and then execute Java-classpath e:\ com.example.Test (the runtime will not find the Test.class file because I will-classpath the < path in the Gt Specified as E:\, which temporarily overwrites the value in CLASSPATH)

[Back to top]


Vii. Summary

1) Javac

    • -sourcepath and-classpath are all related to classpath.
    • If you specify-sourcepath or-classpath, then their < path > will temporarily overwrite the value in Classpath
    • If you do not specify-sourcepath or-classpath, the system will automatically find the appropriate path to CLASSPATH when compiling the program

2) Java

    • -classpath is also associated with classpath.
    • If you specify-classpath, then its < path > will temporarily overwrite the value in Classpath
    • If you do not specify-classpath, the system will automatically find the appropriate path to classpath when you run the program

3) procedure for compiling and running the program

Assuming that the file is located under the E:\net\example\ path, the file name is Person.javapackage net.example;public class person{@Overridepublic String toString ( {return "person";}}
Assuming that the file is located under the D:\sources\ path, the file name is Test.javapackage Com.example;import net.example.person;public class Test{public static void Main (string[] args) {System.out.println (New person ());//reference Person object}}

Compile:

    • Execute Javac-sourcepath e:\-D D:\myclasses D:\sources\Test.java in any directory on the command line
    • At compile time, the system first finds the Test.java file under the D:\sources\ directory according to the above command, which is a. java file that contains the main class.
    • Then according to-sourcepath E:\ And the import Net.example.Person in the Test.java file; The statement found under the E:\net\example\ path has the package net.example; Statement of the Person.java file (without specifying-sourcepath, the system will go to CLASSPATH to find, in fact, the designated-sourcepath, the system is also to go to CLASSPATH search, because the role of-sourcepath is temporarily covered out C Value of Lasspath)
    • Finally, the compiled. class file is stored under the D:\myclasses\ path
    • The entire process: the. java file that contains the main class--classpath--> import statement--finds the. java file that contains the corresponding package statement

Run:

    • Execute Java-classpath D:\myclasses under any directory on the command line Com.example.Test
    • At run time, the system will find Test.class under the D:\myclasses\com\example\ path according to-classpath D:\myclasses and Com.example.Test
    • Then according to-classpath D:\myclasses and import Net.example.Person; The statement found under the D:\myclasses\net\example\ path has the package net.example; Person.class of statements

[Back to top]


Viii. Generating Jar Packages

1) Assume that there is a D:\classes\ path in the computer with A.class, B.class, and c.class three files in the path
2) switch the command line to the D:\classes\ directory and execute the JAR-CVF Test.jar *.class
3) To view the D:\classes\ path, you will find that a Test.jar file is generated, which contains the A.class, B.class, and c.class three files


[Back to top]


end~

Java notes--java and Javac

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.