0% found this document useful (0 votes)
19 views

Building a Java application in Visual Studio Code

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Building a Java application in Visual Studio Code

Uploaded by

patrickmole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

The extension requires a minimum of JDK 11.

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

 JDK_HOME environment variable

 JAVA_HOME environment variable

 current system path

Next, let's explore the generated project.

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:

public class Concatenate {

public static void main(String[] args) {

if (args.length < 2) {

System.out.println("You need to provide 2 lists as arguments. You provided " + args.length);

throw new UnsupportedOperationException("You need to provide 2 lists as arguments");

List<Integer> firstSeries = List.of(args[0].split(",")).stream()

.map(Integer::valueOf).collect(Collectors.toList());

List<Integer> secondSeries = List.of(args[1].split(",")).stream()


.map(Integer::valueOf).collect(Collectors.toList());

List<Integer> elements = extractCommonElements(firstSeries, secondSeries);

System.out.println(elements);

public static List<Integer> extractCommonElements(List<Integer> list1, List<Integer> list2) {

Set<Integer> intersection = new HashSet<>(list1);

intersection.retainAll(list2);

if (list1.get(0).equals(list2.get(0))) {

intersection.add(list1.get(0));

if (list1.get(list1.size() - 1).equals(list2.get(list2.size() - 1))) {

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.

Debugger and Launch Configurations

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.

You might also like