The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_194

Perhaps some people have never heard of Python, but there will be no one who has not heard of Java. As an old programming language with a long history, it is always at the top of TIOBE programming language, and its top position is unshakable. M1 MAC has been released for some time, as the top chip system of the era. Can these two giants make a better match for developers? This time, we try to build a Java development environment in M1 MAC system, and integrate the Springboot framework, which is currently red and purple in the field of Web development. In addition, who said that to play Java, you must use Eclipse or IntelliJ IDEA? We proudly use Vscode.

The first thing to deal with is the choice of OpenJDK. Here we recommend using the Zulu version provided by Azul. It is Azul’s commercially supported OpenJDK commercial release, with a high degree of consistency with the Oracle JDK, and most importantly, it supports the ARM64 architecture, which is a prerequisite for M1 MAC to run Java projects. Download it at www.azul.com/downloads/?… Here we chose the LTS (long term maintenance) version of JDK11, be sure to choose the Arm64 version.

Once the download is complete, double-click install, and Zulu will place the JDK in the following directory:

/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
Copy the code

If you want to, you can configure the environment variables so that you can use multiple JDK versions in the future:

vim ~/.zshrc
Copy the code

Add:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
Copy the code

Save the disk exit, restart the terminal, enter

java -version
Copy the code

Returns:

Openjdk Runtime Environment Zulu11.48+ 21-ca (build 11.0.11+ 9-lts) openJDK 64-bit Server VM Zulu11.48+21-CA (build 11.0.11+ 9-lts, mixed mode) (base) ➜ ~Copy the code

The ARM64 JDK is installed successfully.

Next, install Maven, a management and build tool built specifically for Java projects. It helps you quickly compile, test, package, and publish Java projects.

In general, you can download the binaries from Maven’s official website. A simpler solution is to use Homebrew. Note that the arm64 architecture must be used.

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
Copy the code

Arm64-based Homebrew is installed in the /opt/ Homebrew directory of the system to distinguish it from older x86 versions.

After the installation is successful, enter the following command:

brew -v
Copy the code

The version number will be returned:

(base) ➜  ~ brew -v        
Homebrew 3.0.2  
Homebrew/homebrew-core (git revision ae4118; last commit 2021-02-28)  
Homebrew/homebrew-cask (git revision 9510a9; last commit 2021-05-20)
Copy the code

Indicates that the installation is successful.

Then use the brew command to install Maven:

brew install maven
Copy the code

After the installation is successful, you can run it directly without having to configure environment variables separately:

mvn -version
Copy the code

Return message:

(base) ➜ ~ MVN - version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: / opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec Java version: 11.0.11, vendor: Azul Systems, Inc., the runtime: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home Default locale: zh_CN_#Hans, platform encoding: Utf-8 OS name: "MAC OS X ", version: "11.2.2", ARCH:" AARCH64 ", family: "MAC" (base) ➜ ~Copy the code

The latest stable version is installed by default.

At the same time, run brew List maven to see the maven installation directory, which will be used in a moment:

The brew list (base) ➜ ~ maven/opt/homebrew/Cellar/maven / 3.6.3 _1 / bin/MVN/opt/homebrew/Cellar/maven / 3.6.3 _1 / bin/mvnDebug / opt/homebrew/Cellar/maven / 3.6.3 _1 / bin/mvnyjp/opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/bin/(4 files) / opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/boot/(2 files)/opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/conf/(3 files) / opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/lib/files (70)Copy the code

At this point, we have both installed the JDK and Maven, the two prerequisites for running a Java project.

Let’s configure the M1 MAC development: the most effective tools for Java Vscode, go to the website downloading: code.visualstudio.com/Download#, but it is important to note that must choose arm64 architecture version:

Once you’ve downloaded it, install it, and when you’re done, you need to install two plug-ins to help you run your Java project.

First open the app Store menu on the left and search for: Java Extension Pack. To install, this is a Java extension,

Then, install by searching for: Spring Boot Extension Pack, which is an Extension package for Springboot.

When you install these two extensions, the system will automatically install other dependencies, so just wait.

After the installation is successful, restart vscode, then go to code-> preferences -> Settings from the menu above, search for maven, and click “in settings.json” to edit:

Add the following configuration to the configuration file:

"java.home": "/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home", / / JAVA_HOME / / specified maven configuration file location "Java. Configuration. Maven. UserSettings" : "/ opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/conf/Settings. The XML", / / maven environment variables, designated as MVN ". Maven. The executable path ": "MVN", / / automatic update effect of pom "maven. Pomfile. AutoUpdateEffectivePOM" : true, / / skip testing, do not need to skip can delete "maven. The executable. Options: "- DskipTests", / / when this value is set to true, and configure the "Java. Home", / / the new terminal will automatically add JDK environment variable for "Java. Home" value "of maven. Terminal. UseJavaHome" : Maven.terminal. customEnv": [{"environmentVariable": "JAVA_HOME", // environmentVariable" value": "/ Library/Java/JavaVirtualMachines/Zulu - 11. The JDK/Contents/Home", / / JDK installation path}], / / at build time, Automatically change the Java configuration information of the project, the classpath information ". The Java configuration. UpdateBuildConfiguration ":" automatic ",Copy the code

The main variables are the JDK path and the Maven configuration file path, which correspond to the two newly installed paths: / Library/Java/JavaVirtualMachines/Zulu – 11. The JDK/Contents/Home and/opt/homebrew/Cellar/maven / 3.6.3 _1 / libexec/conf/Settings. The XML

Once configured, the installed extension is ready to use, restart vscode again.

To experience the magic of Springboot, enter the shortcut key:

command + shift + p

Open command mode, type :spring, and select Create Maven project:

Select Springboot version: 2.4.5; Language: Java; GroupId directly defaults to enter; Project Name: MyDemo; Packaging method: Jar; JDK version: 11; Default dependencies: DevTools (code changes hot update, no need to restart), Web (integrated Tomcat, SpringMVC), Lombok (intelligent generation of setters, getters, toString interface, no need to manually generate, code is more simple), Thymeleaf (template engine).

After selecting the package to be introduced, directly press Enter and select the project path in the new pop-up window, until the Spring Boot project is created:

Then import the created mydemo project into vscode’s workspace:

After that, click on the bug icon on the left to add a startup file for the project and select Java:

At this point, you need to wait while vscode imports the dependencies.

After the project is successfully created, you will find that the root directory of the project has a new. Vscode file containing the launch file launch.json:

{// Use IntelliSense to learn about related attributes. // Hover to view descriptions of existing properties. / / for more information, please visit: https://go.microsoft.com/fwlink/?linkid=830387 "version" : "0.2.0," "configurations: [{" type" : "java", "name": "Launch Current File", "request": "launch", "mainClass": "${file}" }, { "type": "java", "name": "Launch MydemoApplication", "request": "launch", "mainClass": "com.example.mydemo.MydemoApplication", "projectName": "mydemo" } ] }Copy the code

With it, we can use the built-in server startup project, now open entry document within the project folder/mydemo/SRC/main/Java/com/example/mydemo/MydemoApplication. Java, changing the appearance of the following:

package com.example.mydemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class MydemoApplication { public static void main(String[] args) { SpringApplication.run(MydemoApplication.class, args); } @requestMapping ("/") public String home() {return "Hey, welcome to M1 MAC "; }}Copy the code

Here we map a simple route to a simple view, and the home page returns a string of text.

Next open the bug icon, click on the console, and then click the triangle arrow to launch the project:

Service starts immediately:

(base) ➜ mydemo git: (master) ✗ / usr/bin/env "/ Library/Internet plug-ins/JavaAppletPlugin plugin/Contents/Home/bin/Java" -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:62011 -Dfile.encoding=UTF-8 -cp /var/folders/5x/gpftd0654bv7zvzyv39449rc0000gp/T/cp_bp8k59s9mveb6o8xc0fjjde5w.jar com.example.mydemo.MydemoApplication . ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.10. RELEASE) the 2021-05-20 23:05:27. 39304-439 the INFO/restartedMain com. Example. Mydemo. MydemoApplication: Starting MydemoApplication on liuyue with PID 39304 (/Users/liuyue/wodfan/work/mydemo/bin/main started by liuyue in / Users/liuyue wodfan/work/mydemo) 23:05:27 2021-05-20. 39304-443 the INFO [restartedMain] com.example.mydemo.MydemoApplication : No active profile set, falling back to default profiles: Default 23:05:27 2021-05-20. 39304-675 the INFO [restartedMain] e.D evToolsPropertyDefaultsPostProcessor: Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2021-05-20 23:05:27.676 INFO 39304 -- [restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For Additional Web Related Logging consider setting the 'logging.level.web' property to 'DEBUG' 2021-05-20 23:05:29.223 INFO 39304 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (HTTP) 23:05:29 2021-05-20. 39304-237 the INFO/restartedMain o.a pache, catalina. Core. StandardService: Starting service [Tomcat] 2021-05-20 23:05:29.237 INFO 39304 -- org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45] 2021-05-20 23:05:29.301 INFO 39304 -- [restartedMain] O.A.C.C.C. [Tomcat].[localhost].[/] : Initializing Spring Embedded WebApplicationContext 2021-05-20 23:05:29.301 INFO 39304 -- [restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: Initialization Completed in 1624 MS 2021-05-20 23:05:29.689 INFO 39304 -- [restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-05-20 23:05:29.975 WARN 39304 - [restartedMain] o.s.b.d.a.OptionalLiveReloadServer : Unable to start LiveReload server 2021-05-20 23:05:30.036 INFO 39304 -- [restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (HTTP) with Context Path '' 2021-05-20 23:05:30.063 INFO 39304 -- [restartedMain] Com. Example. Mydemo. MydemoApplication: Started MydemoApplication in 3.13 seconds (JVM running for 3.838)Copy the code

Open your browser and go to http://localhost:8080

Conclusion: In the past, Java was a serious language. It was staid, dogmatic, meticulous, and conformist. But as time goes on, you can see that it has changed with The Times, with the emergence of “habit over configuration” frameworks like Spring Boot. It completely overturns the characteristics of traditional Java projects, such as heavy, numerous configurations, low development efficiency, complex deployment process and difficult integration of third-party technologies. In M1 MAC system, we can easily write Java code with Vscode, just like writing Python. Finally, here is the project code: gitee.com/QiHanXiBei/…

The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_194