Home > OS >  How to config java version in Visual Studio Code?
How to config java version in Visual Studio Code?

Time:07-28

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?

CodePudding user response:

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

CodePudding user response:

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)

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

  • Related