Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with VSCode and multiple java versions

All company projects I work for use Java 8. However I want to try working with VSCode for Java. The problem is that it asks me for Java 11 to make the Java extension work (plus I want to start doing personal projects with java 11).

Right now in the path (windows environment), I have java 8. If I put java 11 in the path, although it is supposed to be backward compatible, they have deprecated and removed many things so colleagues have told me that it will give many problems.

How can I work with java 8, java 11, and VSCode? What is the configuration that I must have in the path (Windows environment) and how can I configure VSCode so that each project works with its own SDK? I have looked at the documentation for VSCode, but I don't understand it very well.

like image 434
bibu3344 Avatar asked Mar 30 '26 19:03

bibu3344


2 Answers

Machine Enviromment

One thing is your installed jdk, you can have many jdk installed 8,11,16... but it's suggested have jdk11+ installed for working well on vscode.

Project java version

Except your installed jdk versions, each java project can have your own version java setted ( in pom.xml, in build.grable... )

About VSCODE

After installed JDK11+ you can open your java project ( even on java8 version ) and choose both:

  • jdk version you will work on vscode
  • set java version for specific project

It's possible use jdk11 and working in project with Java 8 don't forget to install Extension Pack for Java ( vscjava.vscode-java-pack ) on vscode

How to configure?

press CTRL + SHIFT + P and search for 'configure java runtime' you will got 3 tabs to set:

  • Project JDKs: config for java version on project
  • Java Tooling Runtime:config for java jdk installed ( choose JDK11+ )
  • Installed JDKs:list of jdk installed

if something working bad

Clean your workspace after change settings press CTRL + SHIFT + P and search for 'Clean java Language'. Vscode will be reseted and refreshed

like image 102
Dilermando Lima Avatar answered Apr 01 '26 10:04

Dilermando Lima


The JRE for running the VSCode Java Extension is included in the Red Hat Java Extension, so there is no need to configure java.jdt.ls.home or the deprecated java.home. Install the JDK versions you want to use in your project and specify the version in the files below.

  • No build tools (vscode-java)
    • settings.jsonjava.configuration.runtimes"default": true
  • Gradle (vscode-gradle)
    • (*1) settings.jsonjava.import.gradle.java.home
    • (*2) build.gradlejavasourceCompatibility
  • Maven (vscode-maven)
    • (*1) settings.jsonmaven.terminal.customEnv"JAVA_HOME"
    • (*2) pom.xmlpropertiesmaven.compiler.source (or java.version for Spring Boot)

(*1) Tools Java Home: The settings.json can be configured by workspace (project).
(*2) The java.configuration.runtimes that best matches this version will be used.

settings.json

The settings.json can also be automatically configured with Java Extension Pack Auto Config.

// Project Runtimes (multiple versions)
"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "C:\\Program Files\\java\\jdk1.8.x"
  },
  {
    "name": "JavaSE-17",
    "path": "C:\\Program Files\\java\\jdk-17.0.x"
  },
  {
    "name": "JavaSE-21",
    "path": "C:\\Program Files\\java\\jdk-21.0.x",
    "default": true // Runtime to use for No build tools projects
  }
],
// Gradle Daemon Java Runtime
"java.import.gradle.java.home": "C:\\Program Files\\java\\jdk-21.0.x",
// Maven Environment Variables (for GUI context menu)
"maven.terminal.customEnv": [
  {
    "environmentVariable": "JAVA_HOME",
    "value": "C:\\Program Files\\java\\jdk-21.0.x"
  }
],
like image 35
cypher256 Avatar answered Apr 01 '26 08:04

cypher256



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!