本文介绍了如何在 Visual Studio Code 中查找和更改 Java 编译器选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Visual Studio Code (VSC) 的新手,我来自 Eclipse.VSC 告诉我我的项目有 2 个错误:

与在 Eclipse 中一样,我正在寻找一个首选项窗口来更改 Java 编译器的级别(在我的情况下从 1.5 到 1.8),但我找不到它.我只能在任何地方看到文件.菜单首选项显示了许多设置,但对于 Java 编译器没有显示.

解决方案

VS Code 没有对 Java 项目的内置支持.您需要安装一些 Java 扩展并配置它们以指定正确的 Java JDK 版本,您可以通过设置 JAVA_HOME 环境变量或通过设置 java.home 来实现设置:

安装JDK后,您需要配置您的环境用于 Java 开发.最常用的方法是

来自

您可以修改用户工作区设置.在上面的屏幕截图中,我正在修改我的 User 设置,使 java.home 设置影响所有 Java 项目.您还可以修改 Workspace 设置,为每个工作空间配置不同的 java.home.

点击Edit in settings.json,然后添加java.home(如果java.*显示自动完成).

指定你之前得到的JDK的路径:

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

然后重新启动 VS Code.当 Java 概览 选项卡再次显示时,如果您正确设置了路径,它应该不会再提示您下载 JDK,这意味着它已找到您的 JDK.尝试运行一个非常基本的 HelloWorld.java 应用程序.

窗口

如果您使用的是 Windows,他们会提供一个特殊的安装程序来帮助您入门:

为了帮助您快速入门,我们为 Java 开发人员创建了一个 特殊的 Visual Studio Code 安装程序.

下载 Visual Studio Code Java 包安装程序

注意:安装程序目前仅适用于 Windows.对于其他操作系统,请安装这些组件(JDK、VS Code 和 Java 扩展)个别.我们正在开发 macOS 版本,请继续关注.

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

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.

解决方案

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:

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).

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 JAVA_HOME set (echo $JAVA_HOME), I recommend unsetting 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:

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).

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:

这篇关于如何在 Visual Studio Code 中查找和更改 Java 编译器选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 12:52
查看更多