Friday, January 27, 2023

Install JDK

Install JDK

Establishing a JDK development environment is often confusing for those new to Java.
OpenJDK is only the open source code, and many distributions release its binary. When installing JDK on Windows, you must consider three points: JDK version, distribution, and setting environment variables.

JDK Version

Detailed information on each version of the JDK can be found on Wikipedia: Java version history . But usually, we only focus on the LTS (Long-term support) version. The following lists the three main LTS version numbers:

Ver. Date Brief
8 2014-03 Lambdas
Spring 5.3.x & Spring Boot 2.x
11 2018-09 New HTTP Client
Spring 5.3.x & Spring Boot 2.x
17 2021-09 Sealed Classes
Spring Framework 6 & Spring Boot 3

Distributions

Install BellSoft Liberica JDK version 17

In Spring Quickstart Guide official website, it is recommended to use BellSoft Liberica JDK version 17. When entering the BellSoft website to download the SDK, you will find that various versions of JDK are provided. The following examples will use JDK 17 LTS to demonstrate. The downloaded file is in .msi format, so it is easy to follow the steps to complete the installation.

Set windows Environment Variables

If you have not installed other versions of JDK in your Windows environment, the BellSoft installer will automatically add the JAVA_HOME variable and Path to the Windows environment variables. The default information is as follows:

Var. Value
JAVA_HOME C:\Program Files\BellSoft\LibericaJDK-17\
Path C:\Program Files\BellSoft\LibericaJDK-17\bin\

After the installation is complete, open PowerShell and enter the following commands to check the output to make sure whether the installation was successful:

> java --version
openjdk 17.0.6 2023-01-17 LTS
OpenJDK Runtime Environment (build 17.0.6+10-LTS)
OpenJDK 64-Bit Server VM (build 17.0.6+10-LTS, mixed mode, sharing)
> javac --version
javac 17.0.6

Create a “Hello World!” Program

Use spring initializr to create a project quickly. You can set the options through the UI provided by spring initializr and download the project, or directly in the command line of vscode (Ctrl+Shift+P), search for “spring initializr”. Follow the steps to create a project.

In the src\main\java\com\example\demo directory of the project, open DemoApplication.java, and refer to the following example to modify the content:

package  com.example.demo;

import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public  class  DemoApplication {
	public  static  void  main(String[] args) {
	SpringApplication.run(DemoApplication.class, args);
	}
	
	@GetMapping("/hello")
	public  String  hello(@RequestParam(value = "name", defaultValue = "World") String  name) {
		return  String.format("Hello %s!", name);
	}
}

Using terminal (Ctrl+`) and enter:

./mvnw spring-boot:run

Open http://localhost:8080/hello with a browser you will see the Hello World! webpage.


參考資料:


No comments:

Post a Comment

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...