Setting Jdk In Visual Studio Code



JDK (version 1.8.0 or later) VS Code (version 1.30.0 or later) Language Support for Java by Red Hat; Quick Start. Features Set Checkstyle Configuration File. To set the configuration file, Just Right click the.xml file and select Set the Checkstyle Configuration File.

  • In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS). Enter sfdx to filter for commands provided by the Salesforce Extensions. As you use more SFDX commands, those commands will show up in the recently used panel.
  • It works with Language Support for Java by Red Hat to allow users to debug Java code within Visual Studio Code. Starting a debugging session is easy, click on the Run Debug button available at the CodeLens of your main function, or press F5. The debugger will automatically generate the proper configuration for you.

This tutorial shows you how to create a Java web application with Visual Studio Code. You'll learn how to run, debug, and edit the Java web app locally and eventually on the cloud.

Scenario

A simple Spring Boot Getting Started web app

Before you begin

Before running and deploying this sample, you must have the Java SE Development Kit (JDK) and Apache Maven build tools on your local development environment. If you don't have, please install them.

Download and install the Java Extension Pack, which has JDK 11 included.

Note: The JAVA_HOME environment variable must be set to the install location of the JDK to complete this tutorial.

Download Apache Maven version 3 or greater:

Install Apache Maven for your local development environment:

Download and test the Spring Boot app

Clone the Spring Boot Getting Started sample project to your local machine. You can clone a Git repository with the Git: Clone command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). Paste https://github.com/spring-guides/gs-spring-boot.git as the URL of the remote repository and then decide the parent directory under which to put the local repository. After that, open the complete folder within the cloned repository in VS Code by navigating to the folder and typing code ..

Note: You can install Visual Studio Code from https://code.visualstudio.com and Git from https://git-scm.com.

From within VS Code, open any of the Java files within the complete folder (for example srcmainjavahelloApplication.java). If you don't have the Java language extensions installed for VS Code, you will be prompted to install the Microsoft Java Extension Pack. Follow the instructions and reload VS Code after the installation.

Once you have the Java Extension Pack installed, it will automatically build the project for you (this may take several minutes). You can run the application within VS Code by pressing F5 and selecting the Java environment. The Java Debug extension will generate a debugging configuration file launch.json for you under a .vscode folder in your project. You can see build progress in the VS Code Status Bar and when everything is finished, the final active debug configuration is displayed.

You can learn more about how VS Code launches your application in Debugging Launch Configurations. Press F5 again to launch the debugger.

Test the web app by browsing to http://localhost:8080 using a web browser. You should see the following message displayed: 'Greetings from Spring Boot!'.

Make a change

Let's now edit HelloController.java to change 'Greetings from Spring Boot!' to something else like 'Hello World'. VS Code provides a great editing experience for Java, check out Editing and Navigating Code to learn about VS Code's editing and code navigation features.

Click the Restart button on the top of the editor to relaunch the app and see result by reloading the browser.

Debug the application

Set a breakpoint (F9) in the application source code, and reload your browser to hit the breakpoint.

If you would like to learn more about debugging Java with VS Code, you can read Java Debugging.

Congratulations, you have your first Spring Boot web app running locally! Read on to learn how to host it in the cloud.

Deploy Web Apps to the cloud

We just built a Java web application and ran it locally. Now you will learn how to deploy from Visual Studio Code and run it on Azure in the cloud.

If you don't have an Azure subscription, you can sign up for a free Azure account. Create your free Azure account

Install the Azure App Service extension

The Azure App Service extension is used to create, manage, and deploy to Azure App Service with key features including:

  • Create new Azure Web App/Deployment Slot
  • Deploy to Azure Web App/Deployment Slot
  • Start, stop, and restart the Azure Web App/Deployment Slot
  • View a Web App's log files
  • Swap Deployment Slots

To install the Azure App Service extension, open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) and search for azure app service to filter the results. Select the Microsoft Azure App Service extension. For a more command-line Maven-centric experience, you can also check out the Maven plugin for Azure App Service Linux tutorial.

Create a new Web App on Azure

Once the extension is installed, you can take the following steps to create a new Web App on Azure.

  1. Click Create New Project button on the APP SERVICE Explorer view.
  2. Select a subscription.
  3. Enter a unique name for the new Web App.
  4. Select a location for the new Web App.
  5. Select the OS as Linux.
  6. Select the runtime of the Web App, for example Tomcat 8.5 (JRE8).

Build and deploy to a Web App

The deploy process leverages the Azure Account extension (installed along with the Azure App Service extension as a dependency) and you need to sign in with your Azure subscription. If you do not have an Azure subscription, sign up today for a free 30 day account and get $200 in Azure Credits to try out any combination of Azure services.

To sign in to Azure, run Azure: Sign In from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). You can then sign in to your account using the Device Login flow. Select Copy & Open to open your default browser.

Paste in the access code and continue the sign in process.

Once you have signed in, you can open the command prompt or terminal window and build the project using Maven commands. This will generate a new war or jar artifact in the target directory.

After building the project, open the target directory in VS Code Explorer. Right-click on the artifact and choose Deploy to Web App, and follow the prompts to choose the Web App for your deployment.

Open the Output window in VS Code to view the deployment logs. Once the deployment is completed, it will print out the URL for your Web App. Click the link to open it in a browser, you can see the web app running on Azure!

Note: For more advanced features of App Service, you can check out the Azure App Service extension.

Connect with data services

Azure Cosmos DB is a globally distributed database service that allows developers to work with data using a variety of standard APIs, such as SQL, MongoDB, Cassandra, Graph, and Table.

The Spring Boot Starter makes it easy to store data in and retrieve data from your Azure Cosmos DB with SQL API.

Create an Azure Cosmos DB entity on Azure

  1. Go to Azure portal and click the '+' to Create a resource.
  2. Click Databases, and then click Azure Cosmos DB to create your database.
  3. Select SQL (Document DB) API and type in other information for your database.
  4. Navigate to the database you have created, click Keys, and copy your URI and PRIMARY KEY for your database.

Config your project

  1. You can start from the Spring Data Azure Cosmos DB Sample Project.

  2. Navigate to src/main/resources and open application.properties. Replace below properties in application.properties with information of your database.

Run and debug the application

You can press F5 to run your application. To check the result, open Azure portal and access your Cosmos DB. Click Data Explorer, and next choose Documents. You will see data being shown if it is successfully written into Cosmos DB. You can also browse your data entries in Cosmos DB with Azure Databases extension.

After setting a breakpoint (F9) in your source code, refresh your browser to hit the breakpoint. Details about debugging can be found in Java Debugging

Alternatively, you can also use Maven to package and run your project as steps below:

  1. Navigate to the directory azure-spring-boot and run the command.

  2. Navigate to the directory azure-documentdb-spring-boot-sample and run the command.

Next steps

  • To containerize and deploy a web application, check out the Docker Container Tutorial.
  • To learn more about Java Debugging features, see the Java Debugging Tutorial.

This document will give you an overview of how to manage your Java project in Visual Studio Code.

If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.

Project management

Managing a project in VS Code requires the Project Manager for Java extension. The extension helps manage class paths and dependencies, and create new projects, packages, and classes.

Project view

Project view helps you view your project and its dependencies, and provides entry points for project management tasks. You can switch between a hierarchy view and flat view.

Create a project

You can create a project or source only workload by clicking the + sign on project view, or through the command: Java: Create Java Project.... During creation, VS Code will facilitate installing required extension(s) per your project type, if the extension(s) weren't installed.

Import a project or module

A project or module is imported to a workspace through File > Open Folder or File > Open Workspace menu. VS Code for Java will detect your project type automatically. As a tip, you can run the command Java: Import Java projects in workspace to reimport a project and alert the language server that there were changes to the project, without reloading your window.

Add a dependency

For Maven project, you can add a dependency by clicking the + sign next to Maven Dependencies node in project view.

Add a JAR

JAR file(s) can be added by clicking the + sign next to Referenced Libraries node in project view.

For more details on library, refer to Library Configuration.

Export to JAR

You can export your build to JAR by clicking the sign on project view or run command: Java: Export Jar....

Configure JDK

As Java evolves, it's common that developers work with multiple versions of JDK. To correctly configure your environment and project, you have to know two configurations, java.configuration.runtimes and java.home. The former specifies options for your project's execution environment; the latter specifies your language server's execution environment.

Note: Although the Java language server requires JDK version 11 or above to run, this is NOT a requirement to your project's runtime.

The easiest way for you to configure the JDK is to use the Java Runtime Configuration wizard. You can launch the wizard by opening the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and typing the command Java: Configure Java Runtime, which will bring up the configuration user interface shown below.

If you want to configure without the wizard, please refer below for details.

JDK for projects

Jdk Setting Path

VS Code will detect the runtime required for your project and choose the appropriate configuration from java.configuration.runtimes.

Runtime names must be one of: 'J2SE-1.5', 'JavaSE-1.6', 'JavaSE-1.7', 'JavaSE-1.8', 'JavaSE-9', 'JavaSE-10', 'JavaSE-11', 'JavaSE-12', 'JavaSE-13', 'JavaSE-14', 'JavaSE-15'. We will update the list with each supported release of the JDK.

Note: To enable Java preview features, see How can I use VS Code with new Java versions.

JDK for source only

If you only work with source files and don't use a build tool, VS Code will apply an appropriate runtime using the default configuration of java.configuration.runtimes. If a default isn't specified, VS Code will use the runtime used by the language server, which is determined by the order shown below:

Library configuration

Behind the scene, there's a setting java.project.referencedLibaries in settings.json. Below are details on how to customize this setting.

Include libraries

The libraries to reference are described using a set of glob patterns.

For example:

The settings above will add all .jar files in workspace's library folder along with foo.jar from a specified absolute path to the project's external dependencies.

The referenced libraries are then watched by VS Code, and the project will be refreshed if there is a change to any of these dependent files.

By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar.

Exclude some libraries

If you want to exclude some libraries from the project, you can expand java.project.referencedLibraries to use include/exclude fields and add an exclude glob pattern:

In the example above, any binary JAR files in the library/sources folder are ignored as the project's external dependencies.

Attach source jars

By default, a referenced {binary}.jar will try to search {binary}-sources.jar under the same directory, and attach it as source if one match is found.

If you want to manually specify a JAR file as a source attachment, you can provide a key-value map in the sources field:

In this way, bar-src.jar is attached to bar.jar as its source.

In case VS Code throws an error for a classpath issue, try setting your classpath manually by either setting the CLASSPATH environment variable or editing the .classpath file with the path to the JAR file:

In some rare cases, you may need to clean the Java workspace by executing the Java: Clean the java language server workspace command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) to let the language server rebuild your dependencies.

Lightweight Mode

Android Studio Jdk

VS Code for Java supports two modes, lightweight and standard. With lightweight mode, only source files and JDK are resolved by the language server; with standard mode, imported dependencies are resolved and the project is built by the language server. Lightweight mode works best when you need a super quick-to-start and lightweight environment to work with your source files, for example, reading source code, navigating among source code and JDK, viewing outline and Javadoc, and detecting and fixing syntax errors. Also, code completion is supported within the scope of source files and JDK.

Lightweight mode doesn't resolve imported dependencies nor build the project, so it does not support running, debugging, refactoring, linting, or detecting semantic errors. For these features to be available, you need to switch your workspace from lightweight mode to standard mode.

You can control which mode to launch with by configuring java.server.launchMode with the options below:

  • Hybrid (default) - Firstly, a workspace is opened with lightweight mode. You will be asked whether to switch to standard mode if your workspace contains unresolved Java projects. If you choose Later, it will stay in lightweight mode. You can click the server mode icon on the Status bar to manually switch to standard mode.
  • Standard - A workspace is opened with standard mode.
  • LightWeight - A workspace is opened with lightweight mode. You can click the server mode icon on the Status bar to manually switch to standard mode.

The Status bar indicates which mode the current workspace is in using different icons.

  • - workspace opened with lightweight mode.
  • - workspace in the process of being opened with standard mode.
  • - workspace opened with standard mode.

Clicking the lightweight mode icon switches to standard mode.

Build Status

When you edit Java source code in Visual Studio Code, the Java language server is building your workspace to provide you with the necessary language features. You can see the detailed build task status and watch what is happening behind the scene by clicking the language server Status bar icon in the lower right.

Additional resources

Visit the GitHub Repo of the Maven extension for additional configurations and a troubleshooting guide.

Visual

In addition to Maven, there's also a Bazel extension if you use Bazel to build and test your project.

Next steps

Read on to find out more about:

  • Java Editing - Explore the editing features for Java in VS Code.
  • Java Debugging - Find out how to debug your Java project with VS Code.
  • Java Testing - Use VS Code for your JUnit and TestNG cases.
  • Java Extensions - Learn about more useful Java extensions for VS Code.
1/29/2021