Featured image for Java topics.

The 1.0 release of Language Support for Java by Red Hat on Visual Studio Code is now available on VS Code Marketplace. We'd like to take the opportunity to showcase a few workflows that really make it enjoyable to develop Java applications in Visual Studio Code (VS Code).

vscode-java 1.0: The one with Java 17

The extension's 1.0 release supports workflows with Java 17. Released a few weeks ago, Java 17 marks a major point of change in the Java community as many organizations migrate to the latest Java runtime. Here is a short demo around the workflow using the Java extension and Java 17 support:

One of the advantages of the vscode-java extension is that all it takes to start coding is a simple Java source file. There's no need for fancy IDE-specific config files or even build files (such as pom.xml or build.gradle) to get a project started.

Let's take an in-depth tour of the Java extension's new features.

Configuring your Java project

First, we'll begin by opening VS Code in a folder with just a few Java source files, as shown in Figure 1.

A screenshot of the project loaded in VS Code.
Figure 1. Loading the project in VS Code.

Note: One point worth repeating is that we support pretty much any Java runtime for which we can detect a compatible JDK.

As Figure 2 shows, we have configured Java 8, 11, and 17 environments by providing paths to a compatible JDK installation. Although in this example we have set Java 17 as the default for unmanaged projects, you can easily switch to Java 8 by setting it to default: true. Once saved, the configuration will take effect. Maven or Gradle projects will have this configured in their build files; vscode-java can detect and honor those settings.

An animated graphic showing various Java configuration runtimes.
Figure 2. Java configuration runtimes.

With a variety of different code actions, it's very easy to quickly get some boilerplate code. You can generate source headers with java.templates.fileHeader (File → Preferences → Settings), and getters/setters can be auto-generated through code completion. They can also be auto-generated via code actions on the individual fields or class itself, as shown in Figure 3.

create-class-generate-getset
Figure 3. Creating the ​​class to auto-generate getters/setters.

Access references for unnmanaged projects

Having access to the sources of the libraries you're using is important. While we've always supported this for managed projects (e.g., Maven or Gradle), we now support it for unmanaged projects as well. Simply reference a library from Maven Central, and the sources will be available automatically when interacting with some reference from that library, as shown in Figure 4.

If you want a little more control over your unmanaged project's dependencies, but don’t want to transition to using some build tool, you can use java.project.referencedLibraries (File → Preferences → Settings). To jump to a particular method definition, you can use the right-click context menu or configure a keyboard shortcut (e.g., the F12 key).

source-lookup
Figure 4. Source lookup.

Search for methods

It's handy to be able to search for your method declarations the same way you might search for a file, especially when you're dealing with a new project or file structure. Rather than searching for a file that has the method we're interested in, we can search for the method directly. Simply enable java.symbols.includeSourceMethodDeclarations (File → Preferences → Settings).

Type hierarchy

The Java extension also now supports type hierarchy. This feature is accessible from the right-click context menu, but you can also easily configure it as a keyboard shortcut, as shown as Figure 5.

source-method-lookup-type-hierarchy.
Figure 5. Source method lookup type hierarchy.

Run and debug

In order to take advantage of run and debug capabilities, you'll need to install the vscode-java-debug extension from the Visual Studio Code Marketplace.

The Debugger for Java extension lets you run and debug the project in many different ways. For simple projects, you can enable java.debug.settings.enableRunDebugCodeLens (File → Preferences → Settings), and use CodeLens directly from the main method.

You can set breakpoints, inspect and modify variable values, and even interact with the program from the Debug Console, as shown in Figure 6.

run-debug
Figure 6. Run and debug.

Future roadmap

There are many items planned for the Java language support in VS Code. Some of these include:

  • Making it easier to configure compiler error/warnings and code formatting options.
  • Adding useful auto-completion features (such as Postfix completion).
  • Continuing to adopt convenient code actions (new Java language features, for example) and eventually reach feature parity with those provided by the Eclipse Java IDE.
  • Improving the language server's initial startup time and overall performance.
  • Embedding a Java runtime to improve the "Getting Started" experience.

Testimonials

"For me, VS Code for Java is the easiest way to get started with a Java project. It doesn't matter if you're starting from scratch or you need to quickly open an existing project—VSCode for Java is blazingly fast and small. It will work fast even on older machines, and it has all the features you need to be productive when developing Java projects, from old versions of Java up to the latest version: 17!"

—Edson Yanaga, Java Champion and Microsoft MVP, Director of Developer Experience, Red Hat

"vscode-java makes Visual Studio code a fast and lean way to edit Java projects. I can start and stop the editor on any project, small or large, in no time. This makes it a great tool to use as both my primary and secondary IDE while working on JBang and Quarkus projects."

—Max Rydahl Andersen, Distinguished Engineer, Red Hat

"Visual Studio Code and its vscode-java extension have become my go-to solution for a quick and lightweight code exploration and editing experience. Whether it's for examining pull requests, diving into a new code base, or a live demo during a conference talk, vscode-java allows me to work with Java code intuitively and efficiently, providing all the goodies you'd expect from your coding workbench, like code navigation, refactoring support, instant feedback in case of failures, etc. Part of the VSCode extension pack for Java, vscode-java integrates seamlessly with other extensions like the Java debugger and test runner, as well as features like remote development via SSH, making it an invaluable part of my Java toolbox."

—Gunnar Morling, Java Champion, Principal Software Engineer, Red Hat

Conclusion

Check out our Quick Start guide to get started building Java applications using Visual Studio Code. Obviously, 1.0.0 is not the endgame. Development continues, so you can look forward to more goodies—there's a big one coming in the near future.

Give us your feedback

We would love️ to hear what you think about the Java extension. As always, your feedback is critical to our product improvement.

Comments