英文:
VSCode not suggesting and autocompleting java code
问题
我正在VSCode中编写Java程序,注意到提示、错误报告和自动完成功能都无法正常工作。下面的错误通知弹出显示:
抱歉,激活Java的IntelliCode支持时出现问题。请查看“语言支持Java”和“VS IntelliCode”的输出窗口获取详细信息。
我尝试在设置中查找错误,但没有找到任何信息。我还尝试先卸载然后重新安装上述提到的扩展,但没有帮助。
非常感谢任何帮助!
英文:
I was making a java program in VSCode and I noticed that suggestion, error reporting and autocomplete features are not working. A notification showing below error popped up:
Sorry, something went wrong activating IntelliCode support for Java. Please check the "Language
Support for Java" and "VS IntelliCode" output windows for details.
I tried finding the error in settings but couldn't find any. Also I tried first uninstalling and then reinstalling the above mentioned extension, but it didn't help.
Any help much appreciated!!
答案1
得分: 13
如果您使用的是旧版本的Java(低于11),可能会出现此问题。
要解决此问题:
- 打开VScode,进入首选项 -> 设置
- 搜索 java.configuration.runtimes
- 选择 在 settings.json 中编辑,并粘贴以下内容:
"java.configuration.runtimes": [
{
"name": "JavaSE-1.8",
"path": "/path/to/jdk-8"
},
{
"name": "JavaSE-11",
"path": "/path/to/jdk-11",
"default": true
},
{
"name": "JavaSE-14",
"path": "/path/to/jdk-14"
}
]
-
指定您想要在 "path:" 中使用的JDK的路径
通常 macOS 的 JDK 路径为/Library/Java/JavaVirtualMachines/
-
为该 JDK 设置 "default": true
假设您想要使用 JDK 11,在该代码块中设置 "default": true。
有关额外信息,请查阅
此处。
英文:
This problem might occur if you're using an older version of Java (below 11).
To fix this:
> 1. Open VScode and go to Preferences -> Settings
> 2. Search java.configuration.runtimes
> 3. Select Edit in settings.json and paste the follwing:
"java.configuration.runtimes": [
{
"name": "JavaSE-1.8",
"path": "/path/to/jdk-8",
},
{
"name": "JavaSE-11",
"path": "/path/to/jdk-11",
"default": true
},
{
"name": "JavaSE-14",
"path": "/path/to/jdk-14",
},
]
> 4. Specify the path of the JDK which you would like to use in "path:"
>> usually the path of JDK for macOS is
/Library/Java/JavaVirtualMachines/
> 5. Set "default": true for that JDK
Suppose if you would like to use JDK 11, set "default": true in that code block.
Check for additional information
here.
答案2
得分: 6
2022年7月12日更新
Java语言支持扩展现在包含了自己的Java运行时环境,用于启动该扩展。这意味着在大多数情况下,您不需要指定 java.home
和 jdt.ls.java.home
。
您只需要使用设置 java.configuration.runtimes
来指定您在计算机上安装的所有JDK版本。然后,根据您项目的配置,该扩展将选择最合适的JDK版本。
VS Code Java将使用两种类型的JDK。
我猜您的问题是因为语言服务器无法找到有效的JDK来启动自身。(请参阅下一段关于如何修复的内容)
-
第一种类型是用于启动Java语言服务器的JDK,需要 JDK 11 或更高版本。设置
java.home
用于指定用于启动语言服务器的JDK。确保您设置了此项设置,并且Java版本 >= 11。 -
第二种类型是用于构建/启动项目的JDK。这由设置
java.configuration.runtimes
控制,这也在 @Jayan Praveen 的回答中提到。
英文:
Update on July 12, 2022
The Language Support for Java extension now contained a Java runtime by itself, which is used to launch the extension. That means in most cases, you don't need to specify java.home
and jdt.ls.java.home
.
All you need to do is use the setting java.configuration.runtimes
to specify all the JDKs you have installed on your machine. Then the extension will pick the most suitable JDK according to your project's configuration.
There are two kinds of JDKs that VS Code Java will use.
I guess your problem is caused by that the Language Server cannot find a valid JDK to launch itself. (See the next paragraph about how to fix it)
-
The first kind is the JDK that is used to launch the Java Language Server, which requires JDK 11 or higher. And the setting
java.home
is used to specify which JDK is used to launch the Language Server. Make sure you set this setting and the Java version is >= 11. -
The second kind is the JDKs that is used to build/launch your project. That's controlled by the setting
java.configuration.runtimes
, which is also mentioned by @Jayan Praveen's answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论