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!
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
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 { }
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"; } }
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
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>
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
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
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
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!

The difference between VisualStudio and VSCode in performance and resource usage is mainly reflected in: 1. Startup speed: VSCode is faster; 2. Memory usage: VSCode is lower; 3. CPU usage: VisualStudio is higher during compilation and debugging. When choosing, it must be determined based on project requirements and development environment.

VisualStudio (VS) is a powerful integrated development environment (IDE) developed by Microsoft, which supports multiple programming languages, such as C#, C, Python, etc. 1) It provides a rich set of features including code editing, debugging, versioning and testing. 2) VS processes code through powerful editors and debuggers, and supports advanced code analysis and reconstruction using Roslyn and Clang/MSVC compiler platforms. 3) Basic usage is like creating a C# console application, and advanced usage is like implementing polymorphism. 4) Common errors can be debugged by setting breakpoints, viewing output windows, and using instant windows. 5) Performance optimization suggestions include the use of asynchronous programming, code reconstruction and performance analysis.

In VisualStudio, the steps for compiling, testing and deploying the code are as follows: 1. Compiling: Use VisualStudio's compiler options to convert source code into executable files, supporting multiple languages such as C#, C and Python. 2. Testing: Use built-in MSTest and NUnit to perform unit testing to improve code quality and reliability. 3. Deployment: Transfer applications from the development environment to the production environment through web deployment, Azure deployment, etc. to ensure security and performance.

VisualStudioisMicrosoft'sflagshipIDE,supportingmultipleprogramminglanguagesandenhancingcodingefficiency.1)ItoffersfeatureslikeIntelliSenseforcodeprediction,multi-tabbedinterfaceforprojectmanagement,andtoolsfordebugging,refactoring,andversioncontrol.2

The main difference between the free and paid versions of VisualStudio is the richness of features and the service supported. The free version (Community) is suitable for individual developers and small teams, providing basic development tools; the paid version (Professional and Enterprise) provides advanced features such as advanced debugging and team collaboration tools, suitable for large projects and enterprise-level development.

VisualStudioCommunityEdition is a free IDE suitable for individual developers, small teams and educational institutions. 1) It provides functions such as code editing, debugging, testing and version control. 2) Based on the Roslyn compiler platform, it supports multiple programming languages and integrates Git and TFVC. 3) Advanced features include unit testing, optimization suggestions include turning off unnecessary extensions and using a lightweight editor.

VisualStudio is an integrated development environment (IDE) developed by Microsoft, which supports a variety of programming languages, including C#, C, Python, etc. 1. It provides IntelliSense function to help write code quickly. 2. The debugger allows setting breakpoints, step-by-step code execution, and identifying problems. 3. For beginners, creating a simple console application is a great way to get started. 4. Advanced usage includes the application of design patterns such as project management and dependency injection. 5. Common errors can be solved step by step through debugging tools. 6. Performance optimization and best practices include code optimization, version control, code quality inspection and automated testing.

VisualStudio is suitable for large-scale projects and enterprise-level application development, while VSCode is suitable for rapid development and multilingual support. 1. VisualStudio provides a comprehensive IDE environment and supports Microsoft technology stack. 2.VSCode is a lightweight editor that emphasizes flexibility and scalability, and supports cross-platform.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1
Easy-to-use and free code editor
