Java does not take detours tutorial (1. Environment construction)

1. Environment
Setup Before starting to write the first Java program, we need to do some simple preparations.

1.1 Remember DOS?
We can operate our computer through a graphical interface. But as a programmer, you first need to learn to use the command line to operate the computer, because not all computers provide a graphical interface.
In the Windows environment, commands can be executed through DOS.
You only need to remember about 5 commands to complete the basic operation of the computer.

We first start the DOS environment: enter cmd in the address bar of the file browser and press Enter, as shown in the following figure.



The current directory is displayed to the left of the cursor.
The examples in this tutorial are all operated under the C drive, so if it is not the C drive, enter c: Enter to switch to the C drive.

 

Next, we will learn 5 commands:
directory creation,
directory switching
, view the list of files/folders in the current directory,
open the specified file ,
help

Note: directory and folder are a concept, and will not be explained later.

1. Directory creation
command: md directory name
(abbreviation for Make Directory)
Note: The commands are separated by spaces, which will not be explained later.
The directory structure we want to create is as follows:
c:
-work
-001
-002
Enter the following command in the DOS window to create the above directory
md c:\work
md c:\work\001
md c:\work\002

2. Directory switching
command: cd directory name
(abbreviation for Change Directory)
We enter the following command to enter the directory just created
cd c:\work
you can see that the current directory has become c:\work

3. View the list of files/folders in the current directory
Command: dir
(abbreviation for Directory)
Enter dir to display the following

There are four folders: ., .., 001, 002, of
which 001, 002 is the folder we just created
. It is the current directory
.. is the previous directory.

We enter cd.
You can see that it is still in the current directory,
enter cd ..
You can see that you have gone to the upper directory.
Enter cd work\001 and
you can see that you have gone to c:\work\001.
Enter cd ..\..\
and you can see that you have gone to the upper-level directory.

4. Open the specified file
Command: more
We create a file a.txt under Window, the content is abc, and put it in the c:\work directory.

Enter the work directory in the DOS environment and enter the following command
more a.txt

You can see that the content of the file is displayed.

5. Help
input help to display all supported commands in the DOS environment.

If you do not know how to use the command, enter the command name /? to display the usage of the command.
such as dir /?

Start a program
In Window, we double-click a.txt to start Notepad to display the file content.
We can also achieve the above operation under DOS:
enter notepad.exe a.txt
For simplicity, we usually omit .exe
notepad a.txt

In this way, we start Notepad under DOS to open the a.txt file
, where notepad is the program name, and a.txt is the parameter passed to the program. If no parameters are passed, the program
notepad will be opened by default.

WHY
Now we have basically mastered the operation of DOS.
Think about it, why can DOS start the Notepad program after entering notepad?
Yes, DOS needs to know the location of notepad.exe, and then start it.

Let's see if DOS knows where the Notepad program is, enter where notepad and press Enter, as shown below

OK, that is, DOS knows that the notepad program is located in the following path:
C:\Windows\System32\notepad.exe

So where is the path where this program sits set?

In the DOS environment, there is a variable named PATH, which stores the path information of the program.
We enter the following command to view the value of the variable PATH: (the echo command can output the contents of the specified variable, where the variable name is enclosed in %)
echo %PATH%

You can see C:\Windows\system32 in it.
That is to say, when DOS starts a program, it searches for the corresponding file under the variable PATH in turn, and executes the file if it is found. Because it is related to the startup environment, we can call this an environment variable.

So, how to increase or modify the value of the environment variable PATH?
In Windows, you can right-click My Computer -> System Properties -> Advanced System Settings -> Environment Variables, click New or double-click an existing environment variable to modify or add a new environment variable.
Environment variables are separated by semicolons.

 

At this point, we have mastered the DOS basics necessary for writing Java programs, please memorize them by heart.

1.2 Setting up the Java environment

In the previous section, we learned how to start the specified program in the DOS environment. In this section, we will start the Java program in the DOS environment.

First of all, like other Windos programs, we need to install the Java program, download the Java program from Oracle's official website to the local, double-click to complete the installation like other programs, and note down the path of the program installation.
I installed it under C:\Program Files\Java\jdk1.8.0_161

Verify the installation is successful:
Enter the following command under DOS (please replace it with your own installation directory, which will be omitted later)
C:\Program Files\Java\jdk1. 8.0_161\bin\java -version
C:\Program Files\Java\jdk1.8.0_161\bin\javac -version

If the version number is displayed correctly, the installation is successful.
The above java and javac commands exist in the bin directory of the installation directory, where javac is the command for compiling the java program, and java is the command for running the java program.
But it is very inconvenient to enter the full path every time you run it. We can run the Notepad program in the previous section. In any directory, just enter java or javac directly.
Yes, we need to add the installation path to the environment variable PATH.

The new version of JDK will automatically configure the above PATH variables. If you are using an older version of JDK, you need to configure the CLASSPATH environment variable when configuring the PATH.
During the Java compilation process, an environment variable named CLASSPATH will be searched to complete the Java compilation, and the environment variable should contain the class files required for Java compilation.
So we also need to add the following environment variables:
variable name: CLASSPATH
variable value: .;C:\Program Files\Java\jdk1.8.0_161\lib\tools.jar
where . represents the class file in the current directory where the program execution is recorded, tools.jar represents the system class file that loads java.

 

Restart the DOS environment, enter java -version and javac -version to display the correct version number, and complete the Java environment construction.

Note: Although the new version does not require the above configuration, it is recommended that readers do it by themselves according to the above method, and do not omit this step, which is the first step to start the Java program.
Because in the actual environment you will encounter various versions of JDK, many startup loading problems are caused by incorrect environment variables.

Copyright statement: The copyright of this tutorial belongs to java123.vip, and any form of reprinting and citation is prohibited.

Guess you like

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