[Easy to understand] The operating principle and environment variable configuration of Java compiled Class files

1.1  Introduction to Java programs

 

Before starting the case, we need to understand the process of writing a Java application.

From the above figure, we can understand that the program written is roughly as follows;

1.  Source file: write a Java source file (we also call it a source code file), its extension is .java ;

2.  Compile: Then compile the source file into a bytecode file through the compiler, and the bytecode file extension is .class ;

3. Run: Finally use the interpreter to run the bytecode file.

 

Compiling and running operations need to use DOS commands, so after writing the source code file, we need to learn common DOS commands, and then learn to compile and run.

1.2  Source code writing

1.2.1  Create HelloWorld.java file

To write Java source code, you only need to use the most common text editor, such as the notepad that comes with the Windows system . If you don't know what a notepad is , then you can create a text file by right-clicking on the blank space à New à Text Document.

 

Then modify the file name to HelloWorld.java , pay attention to the case of the file name. and whether the file extension has been modified.

 

If you can't see the file suffix, you can find the "View" tab through "Folder Selection" in the "View" menu at the top of the window, and find "Hide extensions for known file types" in the advanced settings of the "View Selection Card" Options, remove the check mark in front, click "OK" to display the file extension. As shown below


 

1.2.2  Write source code

Here is the complete content:

HelloWorld.java

publicclass HelloWorld { 

publicstaticvoid main(String[] args) {  

System.out.println("HelloWorld");

}

}

Note that everyone must pay attention to capitalization when writing source code. All in all, exactly the same.

Below we analyze the source code. The source code of the HelloWorld case is divided into three parts: class , main method , and output statement .

  The class is the outermost part, where public class is the command that must be written, and HelloWorld is the class name, which must be the same as the source file name. Since our source file is named HelloWorld.java , the class name must be HelloWorld . A class has a pair of curly brackets, and the content of the class is stored in the curly brackets. You may ask what is a class, my answer is to ignore what a class is, now you need to write down the following code, which is the syntax for defining a class.

publicclass HelloWorld { 

}

The main method is the content of the class, so the main method is placed in the curly braces of the class. Now we don't need to learn more, just memorize the content of the main method. The main method also has a pair of curly brackets, which are used to store the contents of the main method.

publicstaticvoid main(String[] args) {  

}

 

The output statement is the content of the main method, so the output statement needs to be placed in the curly braces of the main method. The output statement also needs to be memorized. The output statement ends with a pair of parentheses, within which a sentence is enclosed in double quotes: Hello World! . It is output when the program runs.

System.out.println("HelloWorld!");

  

At this point, our source code has been written, and we would like to remind everyone to pay attention to capitalization when writing source code. The file name and file content must be case-sensitive.

 

1.3  Compile the source code file

After learning DOS commands, we can learn to compile source code files. The compilation operation needs to be done using the compiler, which is a part of the JDK when the compiler is installed when the JDK is installed . It can be found in the bin directory under the JDK directory , and its name is: javac.exe .


 

Note that javac.exe does not support double-clicking, so you must use DOS commands to run it.

Let's take a look at the format of using a compiler to compile Java source files:

Format: compiler program full name source file full name

 

Let's analyze it:

My JDK is installed in the D:\develop\Java\ jdk1.7.0_72 directory, then the full name of the compiler is: D:\develop\Java\ jdk1.7.0_72\bin\javac.exe ;

My source file is stored in: D:\java\HelloWorld.java ;

l  The compilation command is: D:\develop\Java\jdk1.7.0_72\bin\javac.exe D:\java\HelloWorld.java .

 

After compilation, a bytecode file will be generated in the same directory as the source file. The extension of the bytecode file is .class .

 

Please note that there will be no output on the console during compilation. If there is output, it means that there is an error in the source code, so once you compile an error, you must check whether there is an error in the source file.

 

1.4  Running the bytecode file

Running the bytecode file requires the java.exe command, which is in the same directory as javac.exe . Like javac.exe , java.exe also does not support double-click to run, so it must also use DOS commands to run. Running a bytecode file is a bit different from compiling:

Enter the directory where the bytecode file is located: that is, enter the D:\java directory;

l D:\develop\Java\jdk1.7.0_72\bin\java.exe HelloWord

Note that " .class " cannot be given when running the HelloWorld.class file , remember! ! !

 

1.5  Frequently Asked Questions about HelloWorld Cases

A: File not found

 a: The file extension is hidden and the compilation fails

 b: The file name is wrong

B: word spelling problem

 a:class is written as Class

 b: String is written as string

 c:System is written as system

 d:main is written as mian

C: bracket matching problem

 a: remove one pair of curly brackets from the class body

 b: Remove one pair of curly brackets from the method body

 c: remove a pair of parentheses from the output statement

 

 

1.6  Simplify compile and run operations

We found that D:\develop\Java\jdk1.7.0_72\bin must be entered every time the program is compiled and run before the javac.exe and java.exe can be used . This method is very troublesome, so, can you not enter D: \develop\Java\jdk1.7.0_72\bin ? Yes, it can be done by configuring Windows ' PATH environment variable.

There are multiple paths stored in the PATH variable. When the program we use in the DOS console only gives the program name without giving the complete path, the Windows system will look for the program in the path saved in the PATH variable. If we save the " D:\develop\Java\jdk1.7.0_72\bin " path to the PATH variable, then when we use javac or java , the Windows system will automatically go to " D:\develop\Java\jdk1. 7.0_72\bin "path to find javac and java .

l Configuration steps:

Right-click Computer  → Properties → Advanced System Settings → Click Environment Variables → Find PATH   in System Variables → Click the Edit button.

 

Configure the JDK installation directory \bin path ( D:\develop\Java\jdk1.7.0_72\bin on my computer ) to the PATH variable, and separate it from other variables with an English semicolon.

 


Note: The access order of files after configuring PATH : first access the current path, if the current path does not have the file, then access the path configured by PATH .

Next let's test:

Delete the original HelloWorld.class ;

Enter the directory where HelloWorld.java is located:

nSwitch  the drive letter to the E drive: C:/>D: ;

Enter the java directory: D:/>cd java ;

nCompile  : D:/java/>javac HelloWorld.java ;

run: D:/java/>java HelloWorld ;

 

 

The second configuration method :

   1. First click the New button in the environment variable

  

  2. Then in the variable name and value write :

 

Note that the variable value is written in the JDK directory installed by yourself , excluding the bin directory

 3. Finally we configure Java_Home into path

 

 

%Java_Home%, which is equivalent to taking out the value in Java_Home , that is, D:\DevelopTools\jdk7u40_64bit we configured earlier

Then combine \bin. In fact, D:\DevelopTools\jdk7u40_64bit\bin is the recommended configuration method , because many java software will use this configuration later.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326283397&siteId=291194637