Overview of Java and setting up the Windows version of Java development environment

image

1. Java Overview

1.1 Java development history

At the end of 1990, SUN (Stanford University Network) predicted that embedded systems would play a major role in the field of home appliances in the future, so SUN established the Green Project led by James Gosling to prepare for the next generation of smart home appliances (TVs, microwave ovens, phones). A universal control system.

At first, the team decided to use C++ language, but C++ at that time did not support cross-platform. Because different home appliances use different chips, changing the home appliances requires modifying the program.

Therefore, James Gosling re-developed a language - Oak based on C++ . Later, because the market demand for smart home appliances was not as high as expected, SUN gave up the plan.

With the development of the Internet in the mid-to-late 1990s , SUN discovered the application prospects of Oak on the Internet, so it transformed the Oak language and officially released it under the name of Java (Java Island, Indonesia) in May 1995.

The characteristics of the Internet are similar to those of home appliances: different computers use different hardware devices (chips).

At that time, in order to make rigid and monotonous static web pages flexible, the industry urgently needed a technology to develop dynamic web pages . This program could be accessed through the Internet and could be cross-platform.

Thanks to the cross-platform nature of Java and the ability to create Applets, Applets are gradually becoming more popular. Applets are a technology that embeds small programs into web pages for execution.

With the rapid development of the Internet and the continuous maturity of Java technology in the Web, it has become preferred development language for Web applications.

Release version publish time Remark
Java1.0 1996/01/23 SUN released the first development toolkit for Java
Java1.1 1997/02/19 The JavaOne conference was held, creating the largest conference of its kind in the world at that time
Java1.2 1998/12/08 Java is split into: J2SE (Java Standard Edition), J2EE (Java Enterprise Edition), J2ME (Java Micro Edition)
Java1.3 2000/05/08
Java1.4 2004/02/06
Java5.0 2004/09/30 ①The version number is directly upgraded from 1.4 to 5.0 ②The Java platform is renamed JavaSE, JavaEE, and JavaME
Java6 2006/12/11
2009/04/20 Oracle acquires SUN for $7.4 billion
Java7 2011/07/02 Oracle releases first Java version after acquiring SUN
Java8 2014/03/18 This version is the biggest change since Java 5.0 and is a long-term support version (LTS)
Java9 2017/09/22 ① Starting from this version, it will be updated every six months ② Java 9.0 does not support 32-bit systems
Java10 2018/03/21 Not a long-term support version
Java11 2018/09/25 The JDK installation package cancels the independent JRE installation package and is a long-term support version (LTS)
Java12 2019/03/19 Not a long-term support version
Java13 2019/09/17 Not a long-term support version
Java14 2020/03 Not a long-term support version
Java15 2020/09 Not a long-term support version
Java16 2021/03 Not a long-term support version
Java17 2021/09 The latest long-term support version, version number is also known as 21.9
Java18 2022/03 Not a long-term support version
Java19 2022/09 Not a long-term support version
Java20 2023/03 Not a long-term support version
Java21 2023/09

Precautions

  1. Java5 and JDK1.5 refer to the same version, Java8 and JDK1.8 refer to the same version

  2. Java will be released by Oracle after Java7

  3. Java8 is the mainstream version currently used by the company

  4. After Java 9 , a version will be released every six months , and a long-term support version will be released every three years . For example, Java8, Java11, and Java17 are long-term support versions.

1.2 Java features

Since the Java language is open source, it has also been supported by many large companies (Oracle, Amazon, Google, Alibaba, HUAWEI, Tencent), and has gradually developed into a simple, easy-to-learn, open source, cross-platform (portable), and architecture It is a development platform and operating platform that is neutral , safe, high-performance, robust, supports object-oriented, multi-threading, distributed, dynamic and many other features.

  • Java is simple and easy to learn: Java is developed on the basis of C/C++. Different from C/C++, Java is a purely object-oriented language. Java abandons the pointers in the C language that are particularly prone to errors and introduces The garbage collection mechanism eliminates the need to manually release memory in most scenarios, and Java also abandons some particularly complex syntax in the C++ language, such as operation overloading, multiple inheritance, etc. Therefore, Java is easier to learn than C/C++

  • Java is open source: Open source refers to open source code. For example, QQ and WeChat are not open source. When downloading QQ, you can only download the installation package but not the source code . Linux, Visual Studio Code, etc. are all open source. When downloading Linux and Visual Studio Code, you can not only download the installation package but also the source code . The advantage of Java open source is that developers can view the specific design and implementation process by reading the source code of the Java language and related frameworks and middleware , thereby improving their own knowledge. techinque level. In the Java world, there are countless open source software and frameworks. Such as Tomcat, Mybatis, Spring Framework, Spring Boot, Spring Cloud, Dubbo, Netty, etc. Even the JDK itself has many implementations, such as Amazon Corretto, Azul Zulu Community, etc.

  • Java is cross-platform: the platform is the operating system (OS). Currently, the mainstream operating systems on the desktop are Windows and macOS, and the server operating systems are Linux (CentOS, Ubuntu Server). The meaning of cross-platform is that projects developed on Windows can run directly on other operating systems (CentOS, Ubuntu Server) with JDK installed without any modification . Different operating systems have different versions of JDK to shield the underlying operations. System differences. The advantage of cross-platform is to save human resource costs, because the project is developed once and adapted to multiple operating systems. When the company develops a project, it will first develop the Java project in the development environment . After the project development is completed, it will be deployed to the test environment for relevant testing. After passing the test, it will be deployed to the production environment to provide external services. Different environments have different operating systems. The development environment uses Windows or macOS to develop Java projects, and the test environment and production environment use Linux to test Java projects or deploy Java projects to provide external services.

  • Java is architecture neutral. Architecture neutrality has two meanings. ① The implementation of JVM only needs to comply with the JVM specifications officially provided by Oracle . However, most Java applications use the HotSpot virtual machine officially provided by Oracle. In addition, some Major Internet companies (HUAWEI, Alibaba, Tencent) will customize JDK according to their own needs. ② In addition to recognizing bytecode files compiled by the Java compiler (javac), the JVM can also recognize bytecode files generated by computer languages ​​such as Groovy, Scala, JRuby, and Kotlin.

1.3 Java technology system platform

Since 1998, Java has been divided into three versions. These three versions are JavaSE, JavaEE ( JakartaEE ), and JavaME. Each version has its own application scenarios (development direction).

  • JavaSE (Java Standard Edition): Java Standard Edition, mainly used for the development of desktop applications . However , most desktop applications are currently developed using C/C++ , and Java is rarely used to develop desktop applications ( IntelliJ IDEA ). JavaSE is The basis of the other two versions (JavaEE, JavaME), learning JavaSE is to lay the foundation for future JavaEE development .

  • JavaEE (Java Enterprise Edition): Java Enterprise Edition is mainly used to develop the backend of large-scale enterprise applications . JavaEE is No. 1 in this field. The so-called enterprise applications are those applications created by commercial organizations and large enterprises, such as Alibaba's The backends of large enterprise applications such as Taobao, Tmall, and Cainiao Logistics are all based on JavaEE.

  • JavaME (Java Micro Edition): Java micro version, mainly used for application development of embedded electronic devices (TVs, refrigerators, microwave ovens, ovens) or small mobile devices (Nokia mobile phones), but with the 3G mobile intelligent platform (Android, With the advent of the era of iOS), especially the rapid popularity of the Android platform with Java as the core programming language, JavaME has been eliminated .

If you go to the recruitment website to search for JavaME , you will find that you cannot find the corresponding position.

image

The relationship between JavaSE, JavaEE, and JavaME

image

1.4 Java application scenarios

Java is mainly good at three development directions: enterprise-level application development (backend development of large-scale distributed systems), big data platform development, and Android application development.

  • Enterprise-level application development : mainly refers to complex large enterprise software systems and various types of website backend systems. Java's security mechanism and its cross-platform advantages are widely used in the development of large-scale distributed systems. Including e-commerce, logistics, finance, telecommunications, social networking, food delivery, travel, etc.

  • Big data platform development : Various big data platform development frameworks include Hadoop, Spark, Flink, etc. In terms of this type of technology ecosystem, there are also various middleware such as Flume, Kakfa, Sqoop, etc. Most of these frameworks and tools are It is written in Java but provides various language APIs such as Java, Scala, Python, R, etc.

  • Android application development : Android applications are written in Java language. The level of Android development largely depends on whether Java is solid.

Java language is the cornerstone language of these three development directions. No matter which development direction you choose, you must first learn the Java language, and then choose the corresponding development direction according to market demand and your own interests.

According to the current market employment situation , most people will generally choose enterprise-level application development (backend development of large-scale distributed systems), and then after accumulating 2-3 years of work experience, they can turn to big data platform development if they are interested .

Basic learning route: JavaSE->JavaEE->Big Data

1.5 JDK and JRE

1.5.1 Introduction to JDK and JRE

  • JDK (Java Development Kit) is called the Java development tool set. JDK includes Java development tools (javac, java, jdb, jconsole, etc.) and JRE. If you want to develop Java programs, you must first install and configure JDK

  • JRE (Java Runtime Environment) is called the Java runtime environment. JRE contains the class libraries needed to run Java programs (mathematics, collections, regular expressions, logs, concurrency, reflection, serialization, network, date time, IO, JDBC ) and JVM (Java Virtual Machine), if you want to run the developed Java program, you must install JRE before , but Java11 will no longer provide a separate JRE download.

Java8 architecture

Java8 architecture

1.5.2 Illustration of the relationship between JDK, JRE and JVM

image

1.5.3 5.3 How to choose the JDK version

First, you need to understand the Oracle Java SE Support Roadmap

LTS means the official long-term support version of Oracle , such as Java8 , Java11, Java17 , Java21, of which Java8 is officially supported until December 2030, Java17 is officially supported until September 2029, and Oracle officially releases an LTS version every 3 years

non-LTS means a version (test version) that is not officially supported by Oracle for a long time, such as Java9, Java10, Java12, Java13, Java14, etc. are all non-LTS versions, which are officially released by Oracle every six months (in March and September each year) A non-LTS version

There are two situations when choosing the JDK version:

  1. When learning Java , you can choose a higher version of JDK, but it must be an LTS version, such as Java17

  2. When you go to the company to develop a Java project , which JDK version is used for the company's existing projects, then you should give priority to which JDK version to use. If the company starts to develop projects from 0, then give priority to choosing a stable version that is officially supported for a long time, such as Java8 or Java17 At present , most of the existing Java projects of Internet companies in China still use Java8, and they will be gradually upgraded to Java17 in the future (2-3 years later). Boot and so on.

image

2. Construction of Java8 development environment for Windows version

2.1 Windows version Java8 download

  1. Visit Java's official website, the access address is https://www.oracle.com/java/

image

  1. Click Download Java in the lower right corner to jump to the Java download page

The download address for Java is https://www.oracle.com/java/technologies/downloads/

Currently (2023/07/12) the latest Java version (JDK version) officially provided by Oracle is Java20 and Java17, but the latest LTS version is Java17

This time we downloaded Java8, so scroll down to download Java8

image

Currently Java8 supports four operating systems

  1. Linux

  2. macOS

  3. Solaris

  4. Windows

When downloading Java8, you need to choose the corresponding version of Java8 according to the version of the operating system you are using.

image

  1. Choose Java8 x64 Installer for Windows

Q: How do I know which Windows version to use?

A: Check the system version information in Settings (Windows+i)->System->System Information->Windows Specifications

image

Since the operating system used is Windows11 2022 H2, select jdk-8u371-windows-x64.exe of Windows x64 Installer

x86 Installer represents the 32-bit Java8 installer

x64 Installer stands for 64-bit Java8 installer

The choice of 32-bit or 64-bit is based on the system type. It is mandatory for all developers to use 64-bit systems , because 32-bit systems can only use 4G memory, and 4G memory is definitely not enough for software development now .

image

4. Download Java8 x64 Installer for Windows

image

5. Register and log in to Oracle account

If you do not have an Oracle account, you can fill in the corresponding information on the registration page to register for an Oracle account.

After the account is successfully created, fill in the account information on the login page to log in to the Oracle account.

image

After successful login, the browser will automatically download Java8

image

When the Java8 download is completed, you will see the Java8 installation package jdk-8u371-windows-x64.exe by default in the download directory (C:\Users\liuguanglei\Downloads).

image

2.2 Windows version Java8 installation

2.2.1 Windows version Java8 installation process

  1. Double-click the Windows version of Java8 installation package jdk-8u371-windows-x64.exe to start the installer

image

In the pop-up message that SmartScreen is currently unavailable, click Run

image

In the **User Account Control that pops up, do you want to allow this app to make changes to your device? **Select Yes

image

  1. Next step

  1. Custom installation

The installation component includes development tools, source code and public JRE by default

The installation path is C:\Program Files\Java\jdk-1.8 by default

Since the JRE is included in the JDK, there is no need to install a public JRE

Therefore, select the public JRE and set it so that this function will not be available.

image

Just install development tools and source code

  1. Click Next

image

start installation

Wait a moment for the installation to be completed

  1. Successful installation

image

After the installation is complete, you can click Next Steps and Close

image

  • Close: Close the installation wizard and complete the Java8 installation

2.2.2 Java8 installation directory for Windows version

The Windows version Java8 installation directory is C:\Program Files\Java\jdk-1.8

Since the JDK includes JRE, there is a jre directory in the Java8 directory

As a Java beginner, you only need to pay attention to the bin directory and the src.zip compressed file

  • bin directory: stores development tools provided by Java, such as javac, java, javap

  • src.zip: stores Java source code files (files ending in .java)

image

For example String.java

image

2.3 Windows version Java8 configuration

2.3.1 Java8 configures the JAVA_HOME environment variable

2.3.1.1 Why does Java8 need to configure the JAVA_HOME environment variable

JAVA_HOME indicates the installation path of Java, such as C:\Program Files\Java\jdk-1.8

image

Later we will use some tools developed by Java, such as Tomcat, Maven, JMeter, etc., and these tools will depend on the JAVA_HOME environment variable. If you do not configure it, it cannot be used normally , and these tools will prompt you to configure the JAVA_HOME environment variable.

For example, when the terminal is located in the bin directory of Maven, when you execute the mvn --version command to view the Maven version information, you will be prompted to configure the JAVA_HOME environment variable

image

And only after configuring the JAVA_HOME environment variable, mvn --version will output maven version information and dependent Java information

image

2.3.1.2 Java8 configures the JAVA_HOME environment variable

  1. Open the system properties panel

Open Settings (Windows+i)>System->System Information>Advanced System Settings

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

Environment variables are divided into user variables and system variables. The difference between the two is that they have different scopes.

User variables are only valid for the current user, and system variables are valid for all users

It is recommended to use system variables

image

  1. Configure the JAVA_HOME environment variable for Java8

Click the New (W) of the system variable, and then enter the variable name and variable value

Variable name: JAVA_HOME

Variable value: C:\Program Files\Java\jdk-1.8

  1. Click three OK in turn to close the system properties panel and complete the Java8 JAVA_HOME environment variable configuration

image

  1. Verify that the JAVA_HOME environment variable configuration of Java8 is successful

The verification method is to start a terminal and enter echo %JAVA_HOME% in the terminal. If the installation path of Java can be output, the configuration is successful.

Q: How to start the terminal

A: Press Windows+R to open the run window

image

Then enter cmd and press Enter

image

You can open the terminal

image

Verify whether the JAVA_HOME environment variable is configured successfully

2.3.2 Configuring Path environment variables in Java8

2.3.2.1 What is the Path environment variable?

The variable name of the Path environment variable is Path, and the variable value is a series of paths, such as %SystemRoot%, %SystemRoot%\system32, etc.

image

Among them, %SystemRoot% represents the Windows system root path C:\Windows

You can enter the echo %SystemRoot% command on the terminal to view

image

The role of the Path environment variable is that when we enter the command on the terminal and press Enter, the system will search for the executable program corresponding to the command from the current path and the path specified by the Path environment variable, and then execute the program.

When we enter the notepad command on the terminal and press Enter

image

The system will open the notepad program corresponding to notepad.

image

The notepad program path corresponding to the notepad command is C:\Windows\notepad.exe

image

But if we enter a command that does not exist (such as notepadxxx), then the system cannot run the executable program corresponding to the command, and then it will prompt that it is not an internal or external command, nor is it an executable program or batch file .

image

2.3.2.2 Use of Path environment variable

Requirement: To enable QQ to be started by entering QQ in any path of the terminal

  1. Find the bin directory of the QQ installation directory, right-click on the QQ shortcut on the desktop, and then select Properties

image

Then click the location where the open file is located in the properties panel (F)

image

Open the bin directory of the QQ installation directory (C:\Program Files (x86)\Tencent\QQ\Bin)

image

  1. Open the system properties panel

Open Settings (Windows+i)>System->System Information>Advanced System Settings

image

Then the system properties panel will pop up

  1. Click on Environment Variables in the System Properties panel

image

  1. Configure QQ’s Path environment variable

First click on the Path of the system variable, then click on Edit

image

Then add QQ’s bin directory (C:\Program Files (x86)\Tencent\QQ\Bin) to the Path environment variable

image

  1. Click OK three times to close the system properties panel and complete QQ’s Path environment variable configuration.

image

  1. Verify whether QQ’s Path environment variable is configured successfully

First restart a terminal, and then enter QQ in any path of the terminal to see if it can be started successfully. If it can be started, it means that the Path environment variable of QQ is configured successfully.

2.3.2.3 Why does Java8 need to configure the Path environment variable?

Assuming that the Path environment variable of Java8 is not configured, when javac and java are used on the terminal, the system will prompt that they are not internal or external commands, nor are they executable programs or batch files .

image

Because the development tools (javac, java) provided by Java8 need to be used on the terminal, if you double-click to run javac and java, they will start the terminal to run and then crash directly .

In order to use javac and java in any path of the terminal , we need to add the bin directory of the Java8 installation directory to the Path environment variable

  • javac is used to compile java source files (files ending with .java). After successful compilation, bytecode files (files ending with .class) will be generated.

  • java is used to interpret and execute bytecode files and run Java programs

2.3.2.4 Configuring Path environment variables in Java8

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

Environment variables are divided into user variables and system variables. The difference between the two is that they have different scopes.
User variables are only valid for the current user, and system variables are valid for all users.
It is recommended to use system variables

  1. Configure the PATH environment variable for Java8

First click on the Path environment variable of system variables, then click Edit

image

Then add the bin directory (C:\Program Files\Java\jdk-1.8\bin) of the Java8 installation directory to the Path environment variable,

image

And pin it to the top

image

  1. Click OK three times in turn, close the system properties panel, and complete the Java8 Path environment variable configuration

image

  1. Verify whether the Path environment variable of Java8 is configured successfully

The way to verify whether the Path environment variable of Java8 is successfully configured is to reopen a terminal, then enter javac -version and java -version in any path of the terminal and press Enter, and then check whether the terminal can output the java compiler version information and java interpreter Version Information

image

2.3.2.5 Java8 uses JAVA_HOME to configure the Path environment variable

Q: Why does Java8 use JAVA_HOME to configure the Path environment variable?

A: When switching JDK versions, you only need to modify the value of the JAVA_HOME environment variable.

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

image

  1. Use JAVA_HOME to configure the Path environment variable of Java8

First click on the Path of the system variable, then click on Edit

image

Then change the Path environment variable C:\Program Files\Java\jdk-1.8\bin of Java8 to %JAVA_HOME%\bin

image

  1. Click OK three times in sequence, close the system properties panel, and complete the Java 8 configuration of the Path environment variable using JAVA_HOME.

image

  1. Verify whether the Path environment variable configuration of Java8 is successful

The way to verify whether the Path environment variable of Java8 is successfully configured is to reopen a terminal, then enter javac -version and java -version in any path of the terminal and press Enter, and then check whether the terminal can output the java compiler version information and java interpreter Version Information

image

2.4 Uninstalling Java 8 on Windows

Q: When do I need to uninstall Java8?

A: If you no longer use it (for example, all company projects are developed using Java17), you can uninstall it with Java8.

Uninstalling Java8 on Windows requires deleting Java8 related environment variables (Path environment variable, JAVA_HOME environment variable) and Java8 programs

2.4.1 Delete Java8 Path environment variable

1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

Then the system properties panel will pop up

  1. Click on Environment Variables in the System Properties panel

image

  1. Delete the Path environment variable of Java8

First select the Path of the system variable, then click Edit

image

Then select the Path environment variable %JAVA_HOME%\bin of Java8, and click Delete

image

  1. Click OK three times in order to close the system properties panel and complete the deletion of the Java8 Path environment variable

image

  1. Verify whether the Path environment variable of Java8 is deleted successfully

Reopen a new terminal and enter javac -version and java -version in any path of the terminal. If the system prompts that javac and java are not internal or external commands, nor are they runnable programs or batch files, the deletion is successful.

2.4.2 Delete Java8 JAVA_HOME environment variable

1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

image

3. Delete the JAVA_HOME environment variable of Java8

Select JAVA_HOME of the system variable and click Delete

image

  1. Click OK twice to close the system properties panel to complete the deletion of the Java8 JAVA_HOME environment variable.

  1. Verify whether the Java8 JAVA_HOME environment variable is successfully deleted

Reopen a terminal, and then enter echo %JAVA_HOME% on the terminal. If the installation path of Java8 cannot be output after pressing Enter, but %JAVA_HOME% is output, it means that the Java8 JAVA_HOME environment variable has been deleted successfully.

2.4.3 Uninstall Java8 program

  1. Open the installed app

Right- click the Start menu, then left-click to select Installed Apps

You can now view installed applications

image

  1. Search installed apps for Java

image

  1. Select Java8 and click Uninstall

Confirm uninstall

image

Then in the User Account Control that pops up , do you want to allow this app to make changes to your device? Just select Yes

The uninstallation will begin and this process will take a few seconds.

image

image

Java8 cannot be found in installed applications after successful uninstallation

image

2.5 What should I do if I charge for Java 8?

Oracle Java archive archives all Java versions

After 2019, subsequent updated versions of Java 8 will start to be charged, but they are mainly targeted at corporate users and will not have any impact on personal learning.

Java SE 8 (8u202 and earlier) is free for production use

Java SE 8 (8u211 and later) requires payment in production environments

Since the Java virtual machine specification is open, any enterprise or individual can implement it

image

The Oracle JDK we commonly use is just a specific implementation of Oralce. Other companies and individuals can develop their own JDK according to the specifications.

Common JDKs include the following types:

  1. Oracle JDK

  2. Open JDK

  3. Foreign IBM, Amazon, SAP, Eclipse, and Azul as well as domestic Alibaba, Tencent, and Huawei all have their own JDK

image

Even if Oracle's Java8 is charged in the production environment, we can also choose free versions of Java8 from other companies, such as Amazon Corretto, Azul Zulu, etc.

3. Construction of Java17 development environment for Windows version

3.1 Windows version Java17 download

  1. Visit Java's official website, the access address is https://www.oracle.com/java/

  1. Click Download Java in the upper right corner to jump to the Java download page.

image

The download address for Java is https://www.oracle.com/java/technologies/downloads/

Currently, the latest Java versions officially provided by Oracle are Java20 and Java17, but the latest LTS version is Java17, so click Java17 on the download page to switch to Java17

Under the Oracle Free Terms and Conditions, JDK 17 binaries are free for use in production environments and can be redistributed free of charge.

image

Java17 currently supports three systems

  • Linux

  • macOS

  • Windows

When downloading Java17, you need to choose the corresponding version of Java17 according to the version of the operating system you are using.

image

  1. Select x64 Installer Java17 for Windows

Q: How do I know which Windows version to use?

A: Check the system version information in Settings (Windows+i)->System->System Information->Windows Specifications

image

Since the operating system used is Windows11 2022 H2, select jdk-17_windows-x64_bin.exe of Windows x64 Installer

image

  1. Download JDK17 x64 Installer for Windows

When you click the https://download.oracle.com/java/17/latest/jdk-17_windows-x64_bin.exe download link, the browser will automatically start downloading the Windows version of JDK17 x64 Installer

image

Compared with Java8, you do not need to agree to the license agreement when downloading, nor do you need to register and log in to an Oracle account, so downloading Java17 is more convenient than downloading Java8.

When the Java17 download is completed, you will see the Java17 installation package jdk-17_windows-x64_bin.exe by default in the download directory (C:\Users\liuguanglei\Downloads).

image

3.2 Windows version Java17 installation

3.2.1 Windows version Java17 installation process

  1. Double-click the installation package jdk-17_windows-x64_bin.exe of the Windows version of Java17 to start the installation program

image

In the pop-up message that SmartScreen is currently unavailable, click Run

image

In the User Account Control popup , do you want to allow this app to make changes to your device? Select Yes to start the installer

  1. Click Next

image

  1. Select destination folder

The default installation path is C:\Program Files\Java\jdk-17

If you want to change to another directory, just click Change , but be careful that the directory does not contain Chinese characters and Chinese spaces.

  1. Click Next

image

Installation in progress. The installation process will take a few seconds.

image

  1. The installation is complete

After Java17 installation is complete, click Next Steps and Finish

image

  • Close: Close the Java17 installation wizard and complete the Java17 installation

3.2.2 Java17 installation directory for Windows version

The default installation path of Java17 for Windows is C:\Program Files\Java\jdk-17

image

Java17 installation directory description

Table of contents illustrate
bin This directory mainly stores various tools of the JDK, such as javac, java, etc.
conf This directory mainly stores JDK related configuration files.
include This directory mainly stores some platform (operating system) specific header files.
jmods This directory mainly stores various modules of JDK.
legal This directory mainly stores authorization documents for various JDK modules.
lib This directory mainly stores some supplementary jar packages of JDK and Java source code (src.zip)

As a Java beginner, you only need to pay attention to the bin directory and lib directory

  • bin directory

image

  • lib directory

image

3.3 Windows version Java17 configuration

3.3.1 Java17 configures the JAVA_HOME environment variable

3.3.1.1 Why Java17 needs to configure the JAVA_HOME environment variable

JAVA_HOME represents the installation path of Java, such as C:\Program Files\Java\jdk-17

image

Later we will use some tools developed by Java, such as Tomcat, Maven, JMeter, etc., and these tools will depend on the JAVA_HOME environment variable. If you do not configure it, it cannot be used normally , and these tools will prompt you to configure the JAVA_HOME environment variable.

For example, when the terminal is located in Tomcat's bin directory and executes the startup.bat script to start Tomcat, you will be prompted to configure the JAVA_HOME environment variable.

image

Only after configuring the JAVA_HOME environment variable and executing the startup.bat script in the bin directory of Tomcat on the terminal can Tomcat be started normally.

image

After successful startup, enter 127.0.0.1:8080 in the browser address bar to access the Tomcat homepage.

image

However, before starting, you need to change the Tomcat log configuration file logging.properties located in the Tomcat configuration file directory (D:\soft\apache-tomcat-10.1.9\conf)

image

About changing the console encoding from the default UTF-8 to GBK, otherwise the terminal will be garbled when it starts.

image

java.util.logging.ConsoleHandler.encoding = GBK

3.3.1.2 Java17 configures the JAVA_HOME environment variable

  1. Open the system properties panel

Open Settings (Windows+i)>System->System Information>Advanced System Settings

Then the system properties panel will pop up

  1. Click on Environment Variables in the System Properties panel

Environment variables are divided into user variables and system variables . The difference between the two is that they have different scopes.

User variables are only valid for the current user, and system variables are valid for all users

Windows computers are usually used by only one person and are unlikely to have multiple users, so it is recommended to use system variables

image

  1. Configure the JAVA_HOME environment variable for Java17

Click the New (W) of the system variable, and then enter the variable name and variable value

Variable name: JAVA_HOME

Variable value: C:\Program Files\Java\jdk-17

image

  1. Click three OK in turn to close the system properties panel and complete the Java8 JAVA_HOME environment variable configuration

image

  1. Verify that the JAVA_HOME environment variable configuration of Java17 is successful

The verification method is to start a terminal , and then enter echo %JAVA_HOME% in the terminal. If the installation path of Java can be output, the configuration is successful.

Q: How to start the terminal

A: Enter cmd in the address bar of the file explorer (Windows+E) and press Enter

image

You can open the terminal

image

Verify whether the JAVA_HOME environment variable is configured successfully

3.3.2 Configuring Path environment variables in Java17

3.3.2.1 Java17 automatically configures Path environment variables

After installing Java17, Java17 will automatically configure the Path environment variable

image

However, the configured path is C:\Program Files\Common Files\Oracle\Java\javapath, and there are only four tools under this path: javac, java, javaw and jshell

This also means that javac and java can be used in any path of the terminal

However, other tools, such as jconsole, jps, jstack, etc., cannot be used in any path of the terminal, so it is recommended to manually configure the Path environment variable of Java17 .

image

3.3.2.2 Manually configure Path environment variable in Java17

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

image

  1. Configure the PATH environment variable for Java17

First click on the Path environment variable of system variables, then click Edit

image

Add the bin directory of the Java17 installation directory (C:\Program Files\Java\jdk-17\bin) to the Path environment variable

image

And pin it to the top

image

Pinned successfully

image

Then delete the Path environment variable automatically configured by Java17

image

  1. Click OK three times in sequence, close the system properties panel, and complete the Path environment variable configuration of Java17

  1. Verify whether the Path environment variable of Java17 is configured successfully

The way to verify whether the Path environment variable of Java17 is successfully configured is to reopen a terminal, then enter javac -version and java -version in any path of the terminal and press Enter, and then check whether the terminal can output the java compiler version information and java interpreter Version Information

image

3.3.2.3 Java17 uses JAVA_HOME to configure the Path environment variable

Q: Why does Java17 use JAVA_HOME to configure the Path environment variable?

A: When switching JDK versions, you only need to modify the value of the JAVA_HOME environment variable.

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

  1. Configure the PATH environment variable for Java17

First click on the Path environment variable of system variables, then click Edit

image

Set the Path environment variable of Java17 to C:\Program Files\Java\jdk-17\bin

image

Change to %JAVA_HOME%\bin

image

  1. Click OK three times in sequence, close the system properties panel, and complete the Java17 configuration of the Path environment variable using JAVA_HOME.

image

  1. Verify whether the Path environment variable configuration of Java17 is successful

The way to verify whether the Path environment variable of Java17 is successfully configured is to reopen a terminal, then enter javac -version and java -version in any path of the terminal and press Enter, and then check whether the terminal can output the java compiler version information and java interpreter Version Information

image

3.4 Uninstall Java 17 for Windows

3.4.1 Instructions for uninstalling Java 17 on Windows

Q: When do I need to uninstall Java17?

A: If you no longer use it (for example, all company projects will be developed using Java21 one day in the future ), you can uninstall it in Java17.

Uninstalling Java17 on Windows requires deleting Java17 related environment variables (Path environment variable, JAVA_HOME environment variable) and Java17 programs

3.4.2 Delete Java17 Path environment variable

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

image

  1. Delete the Path environment variable of Java17

First select the Path of the system variable, then click Edit

image

Then select the Path environment variable %JAVA_HOME%\bin of Java17 and click Delete.

image

  1. Click OK three times to close the system properties panel and complete the deletion of the Path environment variable of Java17.

image

  1. Verify whether the Path environment variable of Java17 is deleted successfully

Reopen a new terminal and enter javac -version and java -version in any path of the terminal. If the system prompts that javac and java are not internal or external commands, nor are they runnable programs or batch files, the deletion is successful.

image

3.4.3 Delete Java17 JAVA_HOME environment variable

  1. Open the system properties panel

Right-click on the Start menu , search for environment variables , and select Edit system environment variables

image

Then the system properties panel will pop up

image

  1. Click on Environment Variables in the System Properties panel

image

  1. Delete the JAVA_HOME environment variable for Java17

Select JAVA_HOME of the system variable and click Delete

image

  1. Click OK twice to close the system properties panel to complete the deletion of the Java8 JAVA_HOME environment variable.

image

  1. Verify whether the Java17 JAVA_HOME environment variable is deleted successfully

Reopen a terminal, and then enter echo %JAVA_HOME% on the terminal. If the installation path of Java17 cannot be output after pressing Enter, but %JAVA_HOME% is output, it means that the Java17 JAVA_HOME environment variable has been deleted successfully.

image

3.4.3 Uninstall Java17 program

  1. Open the installed app

Right- click the Start menu, then left-click to select Installed Apps

image

You can now view installed applications

image

  1. Search installed apps for Java

image

  1. Select Java17 and click Uninstall

image

Confirm uninstall

image

Then in the User Account Control that pops up , do you want to allow this app to make changes to your device? Just select Yes

image

The uninstallation will begin and this process will take a few seconds.

image

image

After successful uninstallation, Java17 cannot be found in the installed applications.

image

3.5 Switching between Java17 and Java8 in Windows version

3.5.1 How to switch between Java17 and Java8

Because Java8 is used when developing projects, but Java17 is installed when learning new features of JDK, so there are two different versions of JDK on the machine at the same time

image

So how do you switch between these two versions?

First you need to know the Java version currently in use

Use javac -version and java -version at any path in the terminal to know that Java17 is currently used.

image

Q: Why is the Java version Java17?

A: Because the value of the JAVA_HOME environment variable is the installation path of Java17, and the JDK’s Path environment variable refers to the JAVA_HOME environment variable.

image

Then if you want to switch between Java8 and Java17, you only need to modify the value of the JAVA_HOME environment variable

3.5.2 Windows version Java17 switches to Java8

If you want to switch from Java17 to Java8 , you only need to change the variable value of the JAVA_HOME environment variable to the installation path of Java8

image

Then reopen a terminal , enter javac -version and java -version on any path of the terminal, and check whether the output version information is Java8.

image

3.5.3 Windows version Java8 switches to Java17

To switch from Java8 to Java17 again , you only need to change the variable value of the JAVA_HOME environment variable to the installation path of Java17

image

Then reopen a terminal , enter javac -version and java -version on any path of the terminal, and check whether the output version information is Java17.

image

4. Development of the first Java program for Windows

4.1 Development process of Java program

image

  1. Writing: Use Java development tools to write source files. The source files end with .java and are used to store Java code.

  2. Compilation: Use javac to compile the source file to generate a bytecode file. The bytecode file ends with .class. The bytecode file name is the same as the class name. The number of bytecode files is the same as the number of classes in the source file. The compiled format Is the javac source file name.java, for example, javac HelloWorld.java

  3. Run : Use java to interpret and run the compiled bytecode file. The format of the run is the name of the java bytecode file (no .class suffix required), such as java HelloWorld

Q: What do you think of Java as a half-compiled and half-interpreted language?

A: Semi-compiled and semi-interpreted means that JVM has both interpreter and JIT compiler to compile and execute.

image

4.2 Introduction to Java development tools

Although JDK provides some tools used in developing Java programs, such as javac, java, jdb, jconsole, etc.

image

However, JDK does not provide development tools for writing Java code , so before writing Java programs, you need to understand Java development tools. I divide Java development tools into two categories, namely text editors and IDEs.

image

Visual Studio Code and Sublime Text are both advanced text editors and are cross-platform (Windows, macOS, Linux). It is recommended that friends who are new to Java can use them to develop Java programs because they are more lightweight than IDEs. , easier for beginners to use.

image

image

IDEA and Eclipse are both IDEs. IDE integrated functions are commonly used in project development, including editors, compilation tools, decompilation tools, testing tools, performance analysis tools, deployment tools, etc., so they can greatly improve development efficiency .

At present, most Internet companies generally use IntelliJ IDEA to develop Java projects, so we will use IntelliJ IDEA throughout the future.

If you are still in college and happen to major in computer science, then there is a high probability that your Java teacher taught you to use Eclipse.

4.3 The first Java program development

The first program you write when learning any programming language is usually called HelloWorld

If you can write and run HelloWorld, it means you have entered the door of programming.

This time we use a relatively primitive development method: that is, based on Notepad + Windows terminal + development tools (javac and java) provided by JDK to develop our first Java program. Although this method is more efficient than using IDE development Low, but it allows beginners to understand the development and operation process of Java programs more intuitively .

  1. Create a new code folder

In the D drive directory, right-click New > Folder

image

Change the name of the folder to code, used to store Java source files

image

  1. Show file extension

By default Windows hides known file extensions

image

It is recommended to check the file extension before creating a new source file so that the file extension can be displayed.

image

  1. Create a new Java source file

Right click on the code folder and create new > text document

image

Rename the default file name of the new text document.txt to HelloWorld.java

Select the new text document.txt, then right-click and select Rename

image

When renaming, the system will prompt that if you change the file extension, the file may become invalid. Do you really want to change it? , click Yes

image

In this way, the HelloWorld.java source file is created successfully.

image

  1. edit source file

Select HelloWorld.java, right-click and select Open with > Select other applications

image

Then choose to open with Notepad

image

After opening the HelloWorld.java source file, you can start writing Java code

image

Then write HelloWorld in the HelloWorld.java source file

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

image

When writing Java code using Notepad

  • If you feel that the code font is too small, you can hold down the Ctrl key and scroll up to enlarge the font.

  • Code needs to be indented appropriately

  • Symbols encountered in the code, such as parentheses (()), square brackets ([]), and curly brackets ({}), need to be written in pairs, and then the content is written inside, and these symbols are all in English , because Java was invented by Americans and does not support Chinese.

After writing, press Ctrl+s to save

image

  1. Compile source files

First, you need to open the Windows terminal, because javac and java can only be used on the Windows terminal.

It is recommended to enter cmd directly in the address bar of the file explorer and press Enter to open the terminal.

image

Since the address bar of the file explorer is located in D:\code, it will automatically switch to D:\code after opening the terminal.

image

Then enter javac HelloWorld.java and press Enter to compile the source file

image

If there are no syntax errors , the HelloWorld.class file will be generated after successful compilation.

image

  1. Interpret and run bytecode files

Enter java HelloWorld on the terminal and press Enter to interpret and execute the HelloWorld.class bytecode file.

If there are no runtime errors, you will see the Windows terminal print out HelloWorld and wrap it in a new line.

image

4.4 Detailed explanation of the first Java program

public class HelloWorld{
	public static void main(String[]args){
		System.out.println("Hello World");
	}
}
  1. Public is a keyword, indicating the access modifier with the highest authority , which is called public. Later, when learning object-oriented, we will also learn about private. The default is protected. Here you only need to remember that if the class name is modified with public, then The class name modified by public must be consistent with the file name .

  2. Class is a keyword used to define a class. For example, class Welcome{} means defining a class. The class name is Welcome. {} means the starting range and ending range of the class. Java is an object-oriented programming language, and classes are Java programs. basic unit.

  3. Multiple classes can be defined in a source file, but only one class can be modified by public. After compilation, the compiler will generate a bytecode file with the same number of classes as in the source file. The bytecode file name is the same as the class name.

    public class World{
    public static void main(String[] args){
    System.out.println(“HelloWorld”);
    }
    }

    class Chinese{

    }

    class American{

    }

After compiling and running the World.java program, three bytecode files will be generated, namely World.class, Chinese.class and American.class, because there are three classes defined in World.java.

image

  1. public static void main(String[] args){} The format is fixed and represents the main method. The main method is the entry point of the Java program. The Java program is executed from the first line of the main method. This method is called by the JVM. If If your Java program wants to run independently, it must have a fixed format main method.

  2. The function of System.out.println(“HelloWorld”); is to print the string HelloWorld on the terminal and then change the line. If you want to output other content, just replace "HelloWorld". ; indicates the end of the statement**, in Java Each statement needs to end with ;, otherwise a compilation error** will occur. For example, System.out.println(); is a print statement. If there is no line break after printing, you can use the System.out.print(); statement

    public class PrintData{

    public static void main(String[]args){
    	System.out.println("HelloWorld");
    	System.out.print("跟光磊学Java");
    	System.out.print("从小白");
    	System.out.print("到架构师");
       	
    }
    

    }

Compile and run the PrintData.java program

image

Since the content printed in the program contains Chinese, but the source file encoding uses UTF-8, and the Windows terminal uses GBK encoding by default, -encoding UTF-8 means using UTF-8 encoding to compile the source file, so that the program output results can be avoided from garbled characters . question.

4.5 Frequently Asked Questions about the First Java Program

4.5.1 Compile-time and run-time errors

Beginners may encounter a variety of errors when developing their first Java program, which can be divided into two categories: compile-time errors and run-time errors.

Because Java programs are compiled first and then interpreted and run, compilation errors occur first, and then interpretation errors occur.

Even if the program compiles successfully ( there is no problem with the syntax ), it does not necessarily mean that your program has no problems at all, because there may be runtime errors.

  • Compile-time errors are syntax errors encountered when compiling Java source files using javac . In addition, there is also the problem of not finding the source file.

  • Runtime errors are errors and exceptions encountered when using java to interpret and execute bytecode files.

If you modify the code of the source file when troubleshooting runtime errors, you must recompile, otherwise your modifications will not take effect.

Don't panic when you encounter an error. Java will tell you where the error is. If you can't solve it, you can also search for answers on the Internet, because most of the errors you encounter may have been encountered by others before.

4.5.2 Common errors and solutions in Java introductory programs

4.5.2.1 Compile-time error: file not found

Case 1: The source file path is incorrect

The HelloWorld.java source file is in the D:\code directory

However, after starting the terminal, it defaults to the C:\Users\liuguanglei directory. If you use javac to compile HelloWorld.java, a file cannot be found error will appear.

image

The solution is to switch to the D:\code directory in the Windows terminal and use javac HelloWorld.java to compile the source file.

  • Switch drive letter: Enter drive letter: (for example, D:) and press Enter

  • Switch path: Use cd path and press Enter. CD can only switch within the drive letter.

image

Case 2: The source file name is incorrect

image

The solution is to change the original file name to the correct one

image

4.5.2.2 Compile time error: symbol not found problem

Error program case

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

image

Solution: Change string to String, because Java grammar stipulates strict case sensitivity , and string and String represent completely different meanings.

image

4.5.2.3 Compile-time error: encoding GBK unmappable character

Error program case

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

image

Solution: Replace the Chinese semicolon; with the English semicolon; because Java does not recognize the Chinese semicolon

image

What everyone needs to pay attention to is that various symbols in Java code must be in English, such as {}, (), [],; are all in English

4.5.2.4 Compile time error: garbled code problem

Error program case

public class HelloWorld{
	public static void main(String[]args){
		System.out.println("跟光磊学Java从小白到架构师");
	}
}

image

The reason for the garbled code is that the encoding and decoding are not the same encoding.

The default encoding for Java source files is UTF-8

image

The encoding of the Windows terminal or command prompt is GB2312

In the Windows terminal, you can use chcp to view the terminal encoding. If the output result is 936, it means GB2312 encoding. GB2312 is a double-byte encoding used to represent simplified Chinese characters.

image

Garbled characters caused by inconsistency between these two codes

Solution 1: Set the encoding to ANSI when saving the file as

File>Save As

image

Set the file encoding to ANSI and click Save

image

When confirming to save as HelloWorld, it will prompt that **HelloWorld.java already exists. Do you want to replace it? **Click Yes

image

At this time, the file encoding is changed from UTF-8 to ANSI. ANSI is the Chinese encoding in the Chinese environment.

image

Then compile and run HelloWorld

image

Solution 2: Specify file encoding at compile time

javac -encoding UTF-8 HelloWorld.java

image

4.5.2.5 Runtime error: Main class cannot be found or cannot be loaded

The main class represents the class with the main method. The reason why the main class cannot be found or cannot be loaded is that there is no specified bytecode file in the current path.

image

Solution: Compile and generate bytecode files first, then run the program

image

4.5.2.6 Runtime error: main method cannot be found

Error program case

public class HelloWorld{
	public static void mian(String[]args){
		System.out.println("跟光磊学Java从小白到架构师");
	}
}

image

Solution: Just change main to main. If a Java program wants to run independently, it must contain a fixed-format main method.

image

Guess you like

Origin blog.csdn.net/Tony_CTO/article/details/131842223