Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and change Java compiler option in Visual Studio Code?

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.

like image 548
Seb57 Avatar asked Jul 02 '19 20:07

Seb57


People also ask

How do I change the compiler path in VS Code?

In the Windows search bar, type 'settings' to open your Windows Settings. Search for Edit environment variables for your account. Choose the Path variable in your User variables and then select Edit. Select New and add the Mingw-w64 destination folder path to the system path.

Does Visual Studio Code have a Java compiler?

VS Code supports code completion and IntelliSense for Java through Language Support for Java™ by Red Hat. It also provides AI-assisted IntelliSense called IntelliCode by putting what you're most likely to use at the top of your completion list. For more details, see Java Code Navigation and Editing.

How do I set the default compiler in VS Code?

From the drop-down next to the play button, select Debug C/C++ File. Choose C/C++: cl.exe build and debug active file from the list of detected compilers on your system (you'll only be asked to choose a compiler the first time you run/debug helloworld. cpp ).


4 Answers

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.

like image 58
Gino Mempin Avatar answered Oct 24 '22 09:10

Gino Mempin


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

like image 26
Mohamed Sulaimaan Sheriff Avatar answered Oct 24 '22 09:10

Mohamed Sulaimaan Sheriff


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.

like image 24
Seb57 Avatar answered Oct 24 '22 11:10

Seb57


Add to .vscode/settings.json:

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

Note: This is only for the current project.

like image 1
edu.dev.ar Avatar answered Oct 24 '22 09:10

edu.dev.ar