Home > Development Tools > VSCode > body text

Let's talk about how to build a Java project in vscode

青灯夜游
Release: 2021-12-01 19:38:45
forward
10450 people have browsed it

How to build a Java project in

vscode? The following article will introduce to you how to build a Java project in vscode. I hope it will be helpful to friends in need!

Let's talk about how to build a Java project in vscode

For many years, Java development has been dominated by three major IDEs - Eclipse, InelliJ IDEA and NetBeans. But we have other good options. Among the growing number of general-purpose multi-language code editors, Visual Studio Code has emerged as a standout, offering impressive Java support. VS Code also provides first-class support for other technology stacks, including front-end JavaScript frameworks, Node.js, and Python.

Should Visual Studio Code be your next Java IDE? This article explains how to use Visual Studio Code to build an enterprise Java backend with Spring and connect it to the Svelte JavaScript frontend.

Setting up Spring Boot

To complete the build for this tutorial, you will need Java and Maven installed. You'll also need to install the latest Visual Studio Code release for your system if you haven't already. This is a simple installation process.

Now let’s jump straight into a new project. You will use Spring Initializr to create a new Spring Boot web application. Open VS Code and click the extension icon in the lower left corner. This will let you search for available plugins (there are many). Type "spring init" and you'll see Spring Initializr Java Support Extension. Install it as shown in Figure 1.

Figure 1. Installing the Spring Initializr extension

Lets talk about how to build a Java project in vscode

Once installed (it won’t take long), you can pass the command To use it, it can be accessed with Ctrl-Shift-P (or View -> Command Palette from the main menu). Open the command line and type "spring init" and you will see the newly installed command. Run it.

Now follow the guide. You can accept most defaults.

When adding dependencies, add Spring Boot Web and Spring DevTools. (You can add more dependencies later by right-clicking on the POM file and selecting "Add Launcher"). You'll also choose a location for the project; just choose a convenient location on your local drive.

Once the new project is created and loaded into your workspace, you can open a command line terminal by entering Ctrl-Shift-` or from Terminal -> New Terminal.

In the terminal, enter mvn spring-boot:run. The first time you do this, Maven will download new dependencies. Once completed, the development server will start running. You can verify this by opening a browser and visiting localhost:8080. You'll see a default "not found" error page because we haven't defined any routes yet, but this verifies that the server is up and listening.

You can press Ctrl-Shift-P and enter "Demo" to call up the DemoApplication.java file to quickly access the file. Open it and you'll see a typical standalone Spring Boot starter application.

Now we are going to install the Java extension pack, which provides us with various features such as IntelliSense and context-sensitive resource creation. Return to the extension menu, enter "Java extension", and then install the Java extension package. Finally, add the Spring Boot extension package. Now you will notice that when you open the DemoApplication.java file, VS Code provides run and debug commands in the source file.

Import Java Project

At this point, Visual Studio Code understands Java and will prompt you. "This project contains Java, do you want to import it?" Go ahead and select "Always". Once you do this, VS Code can do things like autocomplete for Java.

Let’s add a REST controller. Open the file view (top left of the left menu), right-click /src/com/jay/demo, and select "New File". Name the file MyController.java, as shown in Listing 1.

Listing 1. Java in VS Code

package com.jay.demo;
public class MyController {

}
Copy after login

First, annotate this class with @RestController. Please note that after installing the extension, you have full autocomplete support. Also note that you can always request IntelliSense and autocomplete by placing your cursor where you need help and then typing Ctrl-space, which will make VS Code provide suggestions based on your current location. If you've used Eclipse, this will be familiar, this is the same hotkey.

In the new MyController class, start typing "Get..." and you'll get an autocomplete GetMapping snippet; go ahead and select it. This will create a basic GET mapping, which we will modify as shown in Listing 2.

Listing 2 Basic GET mapping

@RestController
public class MyController {
  @GetMapping(value="/")
  public String getMethodName(@RequestParam(required = false) String param) {
      return "test";
  }
}
Copy after login

Now if you open localhost:8080, you will see a simple "test" response. Note that the server is automatically reloading changes due to Spring DevTools and spring-boot:run.

Create a Svelte frontend

Now let's open a new terminal - you can run terminals side by side by selecting Terminal -> Split-Terminal. In a new terminal, go to a convenient directory (not within the Java project) and create a new Svelte front end with the commands shown in Listing 3.

Listing 3 Svelte front-end scaffolding

npx degit sveltejs/template vs-java-frontend
cd vs-java-frontend
npm install
npm run dev
Copy after login

Now you should be able to browse to localhost:5000 and see the Svelte greeting page.

Add the front end to the workspace

Next, right-click under the Demo project in the file explorer and select "Add Folder to Workspace". Navigate to the front-end project we just created with Svelte. This will add the frontend to VS Code as part of the project workspace, so we can edit it. Now add the Svelte for VS Code extension to VS Code, using the same process as above when adding the Java extension. Once the extension is installed, VS Code will be able to handle both front-end JavaScript frameworks and back-end Java.

Connecting the front-end and back-end

We can test the communication between the front-end and the back-end by opening the app.svelte file using Ctrl-Shift-P and modifying the script to look like Listing 4. Listing 4 Backend Communication

<script>
            export let name;
            async function loadData(){
                        let response = await fetch("http://localhost:8080");
                        name = await response.text();
            }
            loadData();
</script>
Copy after login

Listing 4 runs a function that fires a simple GET request to our backend endpoint and puts the response into the name variable, which is then reflected in the markup .

Java Runtime Configuration

To get information about and configure your Java runtime, you can open the command line (Ctrl-Shift-P) and open "Configure Java Runtime". You will see a screen similar to Figure 2.

Figure 2. Configuring the Java runtime

Lets talk about how to build a Java project in vscode

Note that VS Code has detected the JDK you installed and determined which Which version is the project using? It also allows you to install new versions from within the IDE.

Debugging Java

Debugging your Java in VS Code is also very simple. If the demo application is running, stop it. Right-click the DemoApplication file and select Debug. Spring Boot will run in debug mode.

Open MyController, double-click the red dot on the left side of line 14, and set a break point. Now reload the localhost:5000 page. The breakpoint will be caught and you will see a screen like Figure 3.

Figure 3. Debugging a Java file

Lets talk about how to build a Java project in vscode

Note that the menu bar allows you to continue, enter, step over, etc. From here you have full code debugging capabilities, including the ability to get variable status and run commands from the debug console at the bottom.

Run the test

Now open the DemoApplicationTests.java file, which is created by Spring Initializr. Notice that there is a "Run Test" open. Click this. (You can also right-click the file and select "Run Java".)

The test will run and a checkmark will become available - this allows you to view the results of the test run, as shown in Figure 4.

Figure 4. View JUnit results

Lets talk about how to build a Java project in vscode

Save workspace configuration

When you close VS Code, It will prompt you to save the workspace configuration. It is recommended to name it workspace.code-workspace. Save the configuration and when you open the project again you will find that all settings are in place.

VS Code for Java

The Java features found in Visual Studio Code are comparable to those found in more traditional Java IDEs, with the right extensions installed. the difference is. VS Code tends to be lighter, more responsive, and generally just works without any fuss.

This speed and simplicity combined with the ability to seamlessly use other technology stacks - meaning you don't have to switch gears to a new environment or deal with configuration - makes VS Code a go-to for Java development. Compelling choice.

For more knowledge about VSCode, please visit: vscode tutorial! !

The above is the detailed content of Let's talk about how to build a Java project in vscode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template