Java development environment basic configuration

overview

>(green!)

This section mainly covers two parts:

  1. JDK installation
  2. IDEA installation

It is provided for children's shoes who have never touched Java at all, and install the basic development environment before learning Java.

If your environment is almost done, you can also take a general look, some details may need attention.

Install JDK

[-] About JDK

>(green!)

JDK (Java Development Kit) is a Java development kit, which is an indispensable tool for developing Java programs.

For JDK version selection, the major version must be 8, and the minor version can be ignored.

For JDK version selection, the major version must be 8, and the minor version can be ignored.

For JDK version selection, the major version must be 8, and the minor version can be ignored.

JDK download address:

  1. Official website download

  2. Baidu cloud download

    The official website download must register an Oracle account (may also require scientific means), if you do not want to register, you can choose Baidu cloud download

    Baidu cloud download:

    Link: https://pan.baidu.com/s/1FAPY1q2ipauB8DC6vwLpmg

    Extraction code: 1hnm

  3. After downloading the installation package, double-click to install, and the installation process can be directly followed by the next .

A little bit of attention is:

  1. It is not recommended to install the JDK directly on the C drive. It is recommended to find a folder on other disks to store development-related software.

    (This way the development environment will not be lost due to reinstalling the system)

  2. The JDK installation directory should not contain Chinese characters or spaces , which may sometimes cause some strange problems.

  3. A computer can install multiple different versions of JDK at the same time, and you can choose which version to use in the development tool IDEA.

    Of course, we will only use Java 8 in development, and there is no need to install multiple versions of JDK.

Configure JDK (optional)

>(green!)

The significance of configuring the JDK environment variable is to allow the command line cmd to recognize and other commands in any directory java.

But in actual development, we will not use the command line to develop Java, so this step can be omitted, of course, it can also be selected.

We use IDEA for Java development. In fact, there is no need to configure JDK, just understand this section.

how to configure

>(green!)

Here we take the Windows operating system as an example. If it is MacOS, you can configure it on Baidu yourself. If you don’t want to configure it, just forget it.

  1. First, open the JDK installation directory, enter the bin directory, and copy the current path, as follows:
Configure JDK-1
  1. Then right-click My Computer-Properties and find Advanced System Settings :

    Note: After win10 upgrade to 21H1, my computer properties interface has changed. Find the advanced system settings by yourself , and open it as shown in the figure below:

Configure JDK-2
  1. In the above pop-up window, click Advanced -> Environment Variables , as shown below:
Configure JDK-3
  1. Pull down in the window of the system variable below , select Path (not the Path in the user variable above), and then click the edit button, as shown in the figure below:
Configure JDK-4
  1. In the opened window, click the New button, and then paste the bin path just copied, as shown below:

    Note: You can also choose to create a new one here %JAVA_HOME%\bin, and then create parameters outside JAVA_HOMEto point to the jdk installation directory.

Configure JDK-5

Finally, click OK .

>(red!)

Above, the configuration of JDK is completed, and then we can simply test it:

  1. Open the command line tool ( win + R to enter cmd ), enter the command java -version, and the output result is as follows:
cmd test jdk configuration -1
  1. Enter the command javac, the output result is as follows:
cmd test jdk configuration -2
  1. You can also enter commands such as java, java -helpetc., and if you find output results, you can conclude that the configuration is successful.

have to be aware of is:

Some machines are in the directory of the C disk, and the operation command may not display the results normally. If this is the case, you only need to change the directory of a non-C disk.

Why configure

>(green!)

In essence, the JDK environment variable can not be configured at all, and we can briefly understand the reasons for it.

We configure environment variables, in fact, in order to be able to run the relevant instructions of various Java programs in the cmd window. When you run a command in the command line (cmd), if you cannot find a command or file locally, you will find it in the directory declared by path . Similar java, javacetc. commands are obviously not commands that the system itself can execute, because they are neither local commands nor specified paths. So you need to manually configure the path environment variable to point out the path of the files executed by these commands. javaAfter the configuration is complete, you can execute commands such as , etc. in any directory javac.

Finally, a few points are emphasized:

  1. If you do not configure the path environment variable, we can still use the etc. command in the bin directory of the jdk installation directory java. If you are interested, you can try it.
    • At this time, if you want to use javathe command to compile the java file, you must put the java file in this directory.
  2. As long as the path environment variable is configured, the etc. command can be used in any directory Java.
  3. As long as the IDE is used for development, there is no need to set environment variables, because there is no need to manually compile Java files.
  4. In fact, many IDEs (such as IDEA) will have their own JDK. After installing IDEA, Java development can be performed without even installing JDK. However, for the sake of a unified environment, few people will use the JDK that comes with the IDE, but use the local JDK.

first java program

>(green!)

For learning any programming language, there is a consensus in the industry that:

To achieve printing "Hello World!" on the console as the first starter program. Let's implement it now, which will also be your first Java program.

Here is a friendly reminder: If you want to try to manually create a java file and compile and execute it, please be sure to turn on the display of the file extension in the operating system! Please open the file explorer and perform the following operations:

file extension check

Follow the steps below to complete:

  1. First, create a new txt file and name it HelloWorld.java , then you will be prompted to modify the extension and click OK.

    Then write the Java code in the opened text edit box, as follows:

    hello world case
    public class HelloWorld {
           
           
        public static void main(String[] args) {
           
           
            System.out.println("Hello World");
        }
    }
    

    For now, we can use the above program as a standard paradigm for writing Java code, namely:

    1. First define a public modified class, and the class must have the same name as the file name!
    2. Then write the main method (the format is fixed, just copy directly)
    3. Finally, write the code to be executed in the main method .

    It needs to be clear: the main method is the entry method of the Java program, any code to be executed is either written directly in the main method, or indirectly called and executed by the main method!

  2. Then open the cmd command line window and enter the directory where the Java file is stored (relevant operations need to use dos commands, Baidu by yourself)

  3. Use javacthe command to compile the java file (source code file) to get the class file (bytecode file)

  4. Use javathe instruction to interpret and execute the class file (bytecode file) to get the result

The entire operation process is easy to find the corresponding video on the Internet, so I won’t repeat it here. After running javacthe command , if there is no error in the successful compilation, a HelloWorld.class file will appear. If there is a syntax error, the relevant error reason will be given. javaThe instruction is followed by the class name in the java file (not the file name). For example, HelloWorld is the class name, just directly java HelloWorld.

Dynamic Gif demo:

helloworld demo case

The above process is only used as a reference for beginners. The focus is on understanding the process in the figure below, which does not have much significance for actual development.

Java program operation principle

In actual development, Java programmers will not use the command line to compile and execute source code, because it is too inefficient.

Install IDEA

[-] About IDEA

>(green!)

IDEA stands for IntelliJ IDEA, which is an integrated environment for Java programming language development. IDEA has long been recognized as the best Java development tool in the industry, especially in intelligent code assistants, automatic code prompts, refactoring, JavaEE support, various version tools (git, svn, etc.), JUnit, CVS integration, code analysis, The innovative GUI design and other functions can be said to be epoch-making. The IDEA development company is JetBrains, a company headquartered in Prague, the capital of the Czech Republic. In addition to Java development tools, the company also involves products in the development fields such as Python and front-end. It can be said that it is one of the companies that programmers must know. .

IDEA itself is a paid software, and the version changes quickly. In the actual production of the company, in order to avoid some additional problems caused by different tool versions, it is often necessary to unify the tool version number. Here I suggest you:

The version selection of IDEA is recommended to be unified as 2018.3.6!

The version selection of IDEA is recommended to be unified as 2018.3.6!

The version selection of IDEA is recommended to be unified as 2018.3.6!

Note:

It is not to say that other versions cannot be used. The unified tool version is considered for the purpose of unifying the environment and avoiding problems caused by environmental differences. After work, you can make adjustments according to the actual requirements of the company, but no matter what, it is not recommended to use the latest version of a software! Taking IDEA as an example, there are three major versions each year, namely 1, 2 and 3. It is recommended to choose the last minor version of the last major version each year for development.

For example, if you want to choose to use the 2018 version of IDEA, choose the last version of the year with a major version number of 3 and a minor version number of 6.

If you think version 18 is too old, you can consider using 2020.3 or 2021.3, just don't use the latest version! ! !

IDEA download address:

  1. Official website download

  2. Baidu cloud download

    Downloading from the official website may require scientific means. If you want to download faster, it is recommended to use Baidu Cloud Disk to download.

    Baidu cloud link:

    Link: https://pan.baidu.com/s/1MgINzHbzh4e_Z_mmFgEMag
    Extraction code: yo7m

  3. After downloading the installation package, just double-click to install it, and the whole installation process is just the next step !

A little bit of attention is:

  1. Do not install it on the C drive. It is recommended to find a folder on other disks to store development-related software. (This way the development environment will not be lost due to reinstalling the system)
  2. Do not include Chinese in the installation directory , and do not have spaces , which sometimes cause some strange problems.

Use of IDEA (important)

>(green!)

IDEA is to Java programmers what a sword is to generals. While learning the syntax features of Java, you should also pay attention to learning the use of IDEA!

The first line of IDEA code

>(green!)

" Here is an example of using IDEA to implement the "Hello World" case as the basis of IDEA~ "

create a project

[-] Project about IDEA

>(green!)

Project, that is, engineering. It is the top-level concept of IDEA for project structure management. Java development in IDEA must be carried out in a Project.

IDEA's Project has the following characteristics:

  1. Project is only the top-level concept of project management, not a structure that physically exists on the operating system. (you will understand this later)
  2. At the operating system level, creating a project is actually creating a new folder.
  3. From the IDEA software level, creating a project is creating an independent workspace.

Of course, if you want to fully understand the meaning of Project in IDEA, you need to use it more and understand it more.

Creating a Project in IDEA follows the steps below:

  1. Open File—>New—>Project or Create New Project (anyway, you want to create a new Project)

  2. In the pop-up window interface, you will first be asked to select the JDK version, please select the version Java8 , and you can also select a template, but the JavaSE part does not need to select a template, go to the next step directly:

    New Project

    Note: The JDK version selected here is the JDK you installed locally. Specifically, you need to directly select the entire JDK directory, as shown in the figure below:

    IDEA selects the JDK directory
    1. The next step is to name the Project. Let’s talk a little bit about the naming of the Project:
    Create a new Project—Name it

    About the naming problem of Project

    There are no fixed restrictions on the naming format of the project. Generally, it is enough to "see the name and know the meaning" , that is to say, the name of the project needs to indicate the meaning of the project. According to the general laws of daily work, several naming restrictions are given:

    1. Try to use correct English words for the project name, and do not use Chinese or pinyin unless there are special requirements.
  3. It is recommended to use underscores or dashes to connect multiple words.

  4. It is best to know the function and significance of a project through the name, that is, "see the name and know the meaning".

For example, if I create a new project for homework, and I am Java38th, so I can name the project Java38th-homework , which is a good name. Of course, in actual development, it can be decided according to the arrangement of the company's leaders.

  1. Other things to pay attention to when creating a new project:

    1. In addition to the name, the meaning of Location is the hard disk location where the project is stored. It is recommended to find a separate space for storage instead of the default location.
      2. The rest of the settings are default, no need to change, just click Finish .
      3. After the creation is complete, you can choose to open the Project in the current window or in a new window. This is a free choice.

It is strongly recommended that you find a dedicated hard disk area for storage idea-project. As a human being, you need to clean up your room frequently, and as a programmer, you must also manage the contents of your hard disk in an orderly manner. Do not directly use the default path (C drive or root directory) and the default name (untitled) to create a Project!

HelloWorld!

>(green!)

After creating a new Project, we can open the Project page, as shown in the following figure:

New Project interface

Here I created a project named test** under the directory **"E\idea_space". In fact, when the preparations are here, we can start writing Java code.

>(red!)

All Java source codes in IDEA must be placed in the src directory before they can be executed. As shown below:

src directory

Note: In short, src is the abbreviation of the English word "source", which refers to "source code". This abbreviation will be seen and used frequently in the future.

As far as the learning progress of the JavaSE stage is concerned, all codes need to be written in the src directory, and only the code placed in the src directory can be executed.

After knowing **"src directory"**, we proceed as follows:

Right-click the src directory, select New ----> Java Class to create a new class, and then write the main method as before. The specific code is as follows:

hello world case
//文件 HelloWorld.java中
public class HelloWorld {
     
     
public static void main(String[] args) {
     
     
  System.out.println("Hello World");
}
}

In an integrated development environment such as IDEA, the role of the cmd window console has been integrated. Click Run on the left of the main method, start the main method, and you can see the code execution result!

Finally, although it has already been emphasized, there are still a few points to emphasize here:

  1. The class name of the public modified class must be consistent with the file name. There is only one public modified class in a Java file, but there can be multiple non-public classes.

    For example, the following Java code is allowed:

    One Java file defines multiple classes
     //文件 HelloWorld.java中
    public class HelloWorld {
           
           
     }
    class A{
           
           }
     class B{
           
           }
    class C{
           
           }
    

    Note: The meaning of public modifier class, what is public, what is its function and other related issues, we will talk about it later.

  2. The main method is the entry method of the program. Only the Java class with the main method can be started and the code in it can be executed.

    Note: Regarding the concept of method, we will discuss it later.

  3. If Java Classes are placed directly in the src directory, classes with the same name are not allowed, including public modified classes and non-public modified classes! This is like not allowing files with the same name in the same folder. (The concept of a package is involved here, which will be explained in detail later)


Above, the basic use of IDEA is over, let's talk about a more important concept-Module.

Create Module

>(green!)

[-] Module about IDEA

Project is only the top-level concept of IDEA for project management , not a real basic unit. Who is the basic unit of IDEA for Java project development?

Module!

Actually:

Every time a Project is created, a Module will be created by default, and the name of the Module is the same as that of the Project!

Every time a Project is created, a Module will be created by default, and the name of the Module is the same as that of the Project!

Every time a Project is created, a Module will be created by default, and the name of the Module is the same as that of the Project!

Of course, if you want to create a Module in IDEA, you must complete the creation on the basis of a Project.

From the perspective of the operating system, each Module is also an independent folder!

>(red!)

Creating a Module in IDEA follows the steps below:

  1. Right click on any Project—>New—>Module (creating a Module must be based on the Project)

  2. The next steps are roughly the same as creating a Project (because creating a Project is essentially creating a new Module)

    • Choose to choose the JDK version ( choose Java8 )
    • No need to choose a template, just go to the next step
  3. The naming rules of the Module are similar to those of the Project and will not be repeated here.

  4. When creating a Module, you can pay attention to the hierarchical relationship when selecting a path:

    The relationship between modules
    1. If you want the second Module and the first Module to be at the same level, the directory path should reflect the same level.
    2. If you want the second Module and the first Module to have a superior-subordinate relationship, the directory path must reflect the superior-subordinate relationship.
  5. After the above settings are completed, other settings can be kept as default. Finally click Finish to complete the creation of the Module.

So far, you basically know the project management structure in IDEA, and you can start writing code!

The difference between Project and Module

>(green!)

By creating two structures Project and Module in IDEA, it is not difficult to find that there is actually no essential difference between the two.

In general: Project is the conceptual top-level structure, and Module is the basic unit of IDEA.

  1. Project in IDEA is not an independent concept. The essence of creating a new Project is to create an independent Module, and the name of this module is the same as the name of the project.
  2. A Project can have multiple modules
  3. The mainstream large-scale project structure is basically multi-module. This type of project is generally divided into modules according to functions, and the modules can depend on each other.

Two caveats:

  1. In the JavaSE stage, there is no need to create a multi-Module Project. Even if it is created, multiple modules will not actually depend on each other.
  2. Each Project is opened with an independent window, rather than stacked in one window.

Package concept

>(green!)

When writing Java code in IDEA, there is a concept of package.

Packages in Java are somewhat similar to folders in the operating system, but they are different from folders.

Package is a very basic concept, let's explain it below.

package creation

>(green!)

Under IDEA's project (module), you can:

Right-click the src directory --> new --> package to create a package (the picture is no longer shown here, because it is relatively simple).

Notice:

  1. If you enter a string directly, you will create a first-level package name.
  2. If you want to create a multi-level package, then use "." to separate each level of package names.
  3. The string of the package name is not given arbitrarily. For specific specifications, refer to the document: Naming Specifications for Package Names .

The role of the package

>(green!)

Packages in Java have two main functions:

  1. It is used to distinguish Java classes with the same name. There cannot be two Java classes with the same name in the same package. (Two files with the same name are also not allowed in a folder)
  2. Packages can also be used to divide access rights . (Object-oriented details, skip here first)

>(red!)

When writing the "Hello World" case before, I have already emphasized a concept:

There can only be one public modified class in a Java file, and the class name of the public class must be consistent with the name of the Java file.

Therefore, under one Java file, multiple non-public modified classes can actually be defined, so what is the relationship between these non-public classes and public classes?

It's the same package!

So the following class structure is not allowed!

Classes with the same name are not allowed under the same package

**There is a public class Demo under Demo.java, and a non-public class A is not allowed! **Because there is already a public class A under the same package!

Guess you like

Origin blog.csdn.net/gezongbo/article/details/127127739