終於決定在自己電腦裡裝 Java 了。

安裝了 Eclipse Temurin OpenJDK (AdoptOpenJDK) v17, maven, 和設定好了 JAVA_HOME

另外 VS Code 也安裝 Extension Pack for Java 了。

不是 JAVA_HOME 指定那個

然而發現一個 Java maven project, 指定了用 java.version 17, 可在執行時卻不是使用我所安裝的 JDK 17 位置。

在 Sidebar ExplorerJAVA PROJECTS 裡, 發現 JAVA System Library [JavaSE-17] 的路徑是 VS Code 那個 Red Hat Extension 內置的 JRE。

讓我不爽的是,當我對一個 Java Class 點 F12, 本應該能看到內裡的 Definition. 卻發現 Implementation of methods is unavailable

// Failed to get sources. Instead, stub sources have been generated by the disassembler.
// Implementation of methods is unavailable.

中途折騰了一會java.configuration.runtimes,還搞出其他問題:

The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project
XXX cannot be resolved to a type

最後解決方法是:

  1. 在 VS Code 點 F1 , 輸入 Settings, 打開設定檔 settings.json ,加入以下兩句:
{
    "maven.terminal.useJavaHome": true,
    "java.jdt.ls.java.home": "<your_jdk_path>",
}
  1. 再點 F1 , 輸入選擇 Java: Clean Java Language Server Workspace,重開 VS Code 應該就可以了。

如果有人知道更好/正確的方法,希望你能留言告知!

References

https://marketplace.visualstudio.com/items?itemName=redhat.java

https://code.visualstudio.com/docs/java/java-project#_configure-runtime-for-projects

Recommended Posts

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments