Building a Java application in Visual Studio Code
Building a Java application in Visual Studio Code
An IDE (Integrated Development Environment) allows you to quickly program applications by providing
multiple utilities for code development, testing, debugging features, etc. Given the increasing popularity
of Visual Studio Code as a universal IDE, you can easily develop your first Java project by installing the
Oracle Java Platform Extension.
Setup
The Oracle Java Platform Extension is available on Visual Studio Code Marketplace. You can install it
directly from Visual Studio Code by going through the Code menu: Code > Settings > Extensions.
Leverage the command palette to create a new project View > Command Palette > Java: New Project or
open a folder with existing Maven (pom.xml) or Gradle project files (build.gradle, gradle.properties).
Let's create a new Maven project by going through the menu View > Command Palette >Java: New
Project and selecting the Java with Maven option. You will see a prompt asking you to specify a directory
where you wish to save your project (eg: concatenate) and then enter a package name.
The extension will create a basic pom.xml and a default directory structure for a Maven project with the
source folder defined. The pom.xml file is a single configuration file that contains the majority of
information required to build the project.
If no JDK is present in your system then the extension can help you obtain one. In the Visual Studio Code
menu select View > Command Palette > Download, install, and Use JDK and choose to install one of the
JDKs listed there or point the extension to the binaries of an existing JDK on your operating system. This
action will update your user settings. When you would like to use a different JDK, access View >
Command Palette > Preferences:Open User Settings (JSON) and manually update
the jdk.jdkhome setting.
NOTE
You can build, run, and debug your project with the same JDK that runs the Oracle Java Platform
extension. The extension searches for a JDK in the following locations:
jdk.jdkhome
java.home
Project Explorer
You can visualize your project in Visual Studio Code's explorer. The goal of the concatenate project is to
provide the intersection of 2 lists. Your program should receive the lists as arguments and calculate their
intersection. Let's add the following code to achieve that in the Concatenate.java class:
if (args.length < 2) {
.map(Integer::valueOf).collect(Collectors.toList());
System.out.println(elements);
intersection.retainAll(list2);
if (list1.get(0).equals(list2.get(0))) {
intersection.add(list1.get(0));
intersection.add(list1.get(list1.size() - 1));
return intersection.stream().toList();
Copy
In addition to Visual Studio Code's explorer, the Oracle Java Platform extension offers Project
Explorer that contains an overview of the project structure. This feature aims to simplify Java package
structure navigation.
You can use it to clean, compile, test, debug, and execute your Maven/Gradle Java projects.
The Oracle Java Platform Extension launch configuration supports debugging and running Java
applications using JDK11 or newer. You can invoke the launch configuration/debugger when you
select Run main | Debug main code lense or from the Run and Debug activity panel.
Both launch and debugger configurations support the following actions:
Pause
Step over
Step into
Step out
Restart
Stop/Disconnect
In the Run and Debug view you can customize your launch configurations by choosing Java+ from the
dropdown list and then click the run icon.