Bill
Bill

Reputation: 357

How to config java version in Visual Studio Code?

I installed OpenJDK 18 in my Fedora 36 and installed "Extension Pack for Java" on my Visual Studio Code. In my machine, there have 3 versions of java: enter image description here

But when I create a maven project and run it in VS Code, I found it used java 11. enter image description here How can I configure VS code to let it run projects on Java 18?

Upvotes: 6

Views: 47688

Answers (3)

AechoLiu
AechoLiu

Reputation: 18368

For MacOS,

## Install java 11 by brew
brew install java11

## For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

## edit settings.json
"java.configuration.runtimes": [
{
    "name": "JavaSE-11",
    "path": "/opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home",
    "javadoc": "https://docs.oracle.com/en/java/javase/11/docs/api",
    "default": true
}

The "path" could be "/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home". They are the same.

Upvotes: 3

Bill
Bill

Reputation: 357

To config to the right JDK in VS Code. Open File -->Settings --> Java --> Configuration:Runtimes -->[Edit in Setting.json] add:

"java.configuration.runtimes": [
    {
        "name": "JavaSE-18",
        "path": "/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64",
        "sources" : "/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/lib/src.zip",
        "javadoc" : "/usr/share/javadoc/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/api",
        "default":  true
    }
]

The path is the "$JAVA_HOME" if you run $sudo alternatives --config java and the result is

+ 3 java-latest-openjdk.x86_64 (/usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64/bin/java)

then JAVA_HOME is /usr/lib/jvm/java-18-openjdk-18.0.1.1.2-1.rolling.fc36.x86_64

The source, if you install sudo dnf install java-latest-openjdk-src.x86_64 the source code will be $JAVA_HOME/lib/src.zip

To find the Javadoc if you install "java-latest-openjdk-javadoc.x86_64", you can use command rpm -pl java-latest-openjdk-javadoc.x86_64 to check the installation location.

Upvotes: 13

JialeDu
JialeDu

Reputation: 9737

Use Ctrl+Shift+P to open the command palette, search for and select Java: Configure Java Runtime .

enter image description here

or click the three dots after the Java project and select Configure Java Runtime.

enter image description here

Select the version you want from the drop-down options.

enter image description here

Upvotes: 10

Related Questions