32

I am new to Visual Studio Code (VSC) and I come from Eclipse. VSC tells me 2 errors on my project:

  • Build path specifies execution environment J2SE-1.5. There are no JREs installed in the workspace that are strictly compatible with this environment.

  • The compiler compliance specified is 1.5 but a JRE 1.8 is used.

Same as in Eclipse, I am looking for a preferences window to change the level of Java compiler (from 1.5 to 1.8 in my case) but I can't find it. I can only see files everywhere. The menu Preferences show many settings but nothing for the Java compiler.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Seb57
  • 441
  • 2
  • 5
  • 11
  • 1
    Visual Studio Code does not have built-in Java support like Eclipse does. If you are looking for features specific to any language they will be provided by extensions. Are you already using an extension for Java? Have you looked at the documentation/readme for that extension? – Romen Jul 02 '19 at 21:00
  • 1
    This [link](https://github.com/redhat-developer/vscode-java/issues/28) has helpful information. – Gopi Jul 02 '19 at 21:12
  • What is your OS? – Gino Mempin Jul 02 '19 at 21:32
  • Thank for your help. I am on MacOS with Microsoft java Extension pack... – Seb57 Jul 04 '19 at 21:08
  • Using all your answers, I realized that the error was on a project made of Javascript Angular, no need of Java. Then I deleted the directory ".settings" and file ".classpath".. and these 2 errors disappear. – Seb57 Jan 07 '20 at 19:59

8 Answers8

26

VS Code does not have built-in support for Java projects. You need to install some Java extensions and configure them to specify the correct Java JDK version, which you can do by either setting the JAVA_HOME environment variable or by setting the java.home setting:

After installing the JDK, you would need to configure your environment for Java development. The most common way is to set JAVA_HOME environment variable to the install location of the JDK while you can also use java.home setting in Visual Studio Code settings (workspace or user settings) to configure it just for the editor.

I described the installation/setup steps below. It is mainly based on the Visual Studio Code tutorial for Java. Based on the OP's comment, it's focused on the Java extension for VSCode on Mac.


MacOS

First, you need to install the Microsoft Java Extension Pack.

Upon installation, it will display the Java Overview tab, and automatically check for available Java SDKs. If it cannot find one, it will prompt you to download one. (The Java Overview and this JDK Required page seems to also auto-appear when you open/create a .java file).

install JDK

From the VS Code documentation, these JDK distributions should be supported: OpenJDK, Java SE from Oracle, Azul Zulu Enterprise. For this answer, I am using OpenJDK 11. Download and install the appropriate JDK.

Next, get the path to your JDK. Open a Terminal and then:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

If you already have a JAVA_HOME env var set, I recommend not relying on it. I find that it is better to use the java.home setting to configure Java specifically for VS Code (or for a particular workspace).

Now, open the VS Code Settings tab and look for the Java: Home setting:

java: home

You can modify either the User or Workspace setting. In the screenshot above, I am modifying my User settings, making the java.home setting affect all Java projects. You can also modify the Workspace setting to configure java.home differently for each workspace.

Click on the Edit in settings.json, and add java.home (you'll know your extension was installed properly if java.* autocompletion is displayed).

java.home

Specify the path to the JDK you got earlier:

"java.home": "/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home",

Then restart VS Code. When the Java Overview tab displays again, if you set the path correctly, it should not prompt you anymore to download a JDK, meaning it found your JDK. Try running a very basic HelloWorld.java application.

Windows

If you are on Windows, they provide a special installer to help you get started:

To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers.

Download Visual Studio Code Java Pack Installer

Note: The installer is currently only available for Windows. For other OS, please install those components (JDK, VS Code and Java extensions) individually. We're working on the macOS version, please stay tuned.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • I am on MacOS. I tried to add the java.home in settings.json .. and restart VSC . no change. (how to add the snapshot here in comments?). – Seb57 Jul 04 '19 at 21:09
  • 1
    I updated my answer to provide a more detailed step-by-step description for setting-up the Java extension and `java.home`. Check that the path you set is correct. – Gino Mempin Jul 05 '19 at 00:04
  • `java.home` is missing in VS Code settings for Windows. Can't find the way to set it – Konstantin Abakumov Jan 15 '22 at 07:09
  • @KonstantinAbakumov My answer that mentions `java.home` is for MacOS. I unfortunately don't use Windows or have access to a Windows machine, so can't help you there :( – Gino Mempin Jan 15 '22 at 08:22
  • Worked with setting the Env variable, thanks! – Konstantin Abakumov Feb 09 '22 at 08:43
  • 1
    you can find java properties including java.home from terminal `java -XshowSettings:properties -version` – Lekia Mar 30 '22 at 19:30
  • 3
    2023-06-07 update: ```java.home``` is deprecated use ```java.jdt.ls.java.home``` instead – lpkej Jun 07 '23 at 13:01
5

In visual studio code, you need to change your compliance version in below file

  • Filename: org.eclipse.jdt.core.prefs
  • path: Your_project_directory/.settings/org.eclipse.jdt.core.prefs

(.settings will be hidden in file explorer will be visible only in vs code.)

Also, change from 1.7 to 1.8 in the file. 1.8 is the java version based upon the java version input the value

Refer the Image For info

Akshat Zala
  • 710
  • 1
  • 8
  • 23
  • Thanks for your answer. I found the file you said, I changed it to 1.8, restarted VSC but problem still here. I don't know if my changes are taken into account (is restart sufficient ? or shall I send any command ?) – Seb57 Jan 07 '20 at 19:33
  • Shall i know? whether you have built project with maven or used regular java project – Mohamed Sulaimaan Sheriff Jan 09 '20 at 04:38
3

Add to .vscode/settings.json:

{
    "java.jdt.ls.java.home": "<location-of-your-preferred-jdk>"
}

Note: This is only for the current project.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
edu.dev.ar
  • 31
  • 3
  • I had some existing files in the `.vscode/settings.json`, do you think the following insertion is correct: ```json { "workbench.colorCustomizations": { "activityBar.background": "#0D3512", "titleBar.activeBackground": "#124B19", "titleBar.activeForeground": "#F5FDF6" }, "java.jdt.ls.java.home": "C:\\Program Files\\Eclipse Adoptium\\jdk-17.0.5.8-hotspot" } ``` – S.Ale Jan 11 '23 at 16:54
1

Using all your answers, I realized that the error was on a project made of Javascript Angular, no need of Java. Then I deleted the directory ".settings" and file ".classpath".. and these 2 errors disappear.

Thank to all.

Seb57
  • 441
  • 2
  • 5
  • 11
0

Edit: My pom.xml file was not correct. If you use Maven, take a other look on that file.

For me, it was in the ".settings" folder (it should be in your Java project). There's some "org.eclipse" files in there. You just need to change the 1.5 to 1.8.

Picki
  • 469
  • 4
  • 11
0

In my case, I had a jdk8 in environment and I need to compile the java file to class file in Jre 1.7 version. The class files major.minus version need 0x33.

So I need to use the vscode-java to create the class files. But the vscode do not has the setting for compiler. The eclipse project has a setting file in .setting/org.eclipse.jdt.core.prefs. Settings Global Preferences

eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7

This file can change the compiler option in eclipse jdt.

The vscode-java extension has a setting option to use specific setting file. We can add the code below to the .vscode/setting.json to change the java compiler target version.

"java.settings.url": ".settings/org.eclipse.jdt.core.prefs",

As a result the class file compiled has a 0x33 jre major.minor version.

0

Looks like there are a lot of answers here. IMO it's a little confusing because it seems like there are multiple Java Plugins, and multiple ways to set the JDK / JVM for each plugin.

I seem to be using the Redhat plugin. If using the Redhat plugin and a JDK < 17 then I found the way to set JVMs here: https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#java.configuration.runtimes

discord
  • 59
  • 10
0

Seems like setting the array "java.configuration.runtimes" in the settings.json file to all the JDKs you've installed lets VS Code know which JDKs you have installed on your system. Then when you open a new project that enforces sourceCompatibility, you will receive a notification asking whether to use the appropriate JDK (which VS Code now knows about). If you don't see this notification, you could just edit the "java.import.gradle.java.home" setting yourself to point to the correct JDK.

Dinanjanan
  • 399
  • 2
  • 11