3

My VS Code version-1.55.2 Java path in system->>C:\Program Files\Java\jdk1.8.0_291

I am importing java project in vs code for the first time. Below items already tried:

  1. Java clean server workspace
  2. Checked java log server settings in command pallete and it showing its going to jdk11.
  3. Java Pack extension is already installed.

Attaching image of error, i am getting.

How can i point my code to my jdk 8 location. I used to do same in eclipse IDE in past.enter image description here

I understand that vs code now does not pick jdk 8 and has to be jdk 11.

My settings.json file in vs code as below:

{
    "liveServer.settings.useLocalIp": true,
    "liveServer.settings.CustomBrowser": "chrome",
    "editor.minimap.enabled": false,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "files.exclude": {
        "**/.classpath": true,
        "**/.project": true,
        "**/.settings": true,
        "**/.factorypath": true
    },
    "java.home": "C:\\Program Files\\Java\\jdk-11.0.6"
}

and JAVA_HOME is set to same jdk. what else i can try to fix this?

After setting java configuration runtimes below is error i am seeing with my project structure:

enter image description here

Below is my project structure: enter image description here

Avneet
  • 393
  • 4
  • 15
  • Did you try define "java.configuration.runtimes" setting pointing to your JDK 8 as I showed in my answer? – Leandro Arruda May 02 '21 at 17:39
  • Yes, i did set it. – Avneet May 02 '21 at 17:49
  • Did you import the project folder or just src folder? Overcoming things of the extension configuration in a hello world project, check if you have all artefacts about your Eclipse project (project files like pom.xml of Maven or things of ant, gradle, whatever you used when begun your project) . – Leandro Arruda May 02 '21 at 18:10
  • I imported the whole project folder. Folder structure image, I am putting in question. I see it does not contain pom.xml or ant or gradle configuration. seems it just pick from java 8 libraries like in old world, we used to build project in eclipse. – Avneet May 02 '21 at 18:22
  • Adding I did write and run sample Helloworld.java file and it ran successfully so definitely something is missing in this project build. Run time is pointing now to Java 1.8 as per your suggestion. – Avneet May 02 '21 at 18:26
  • 3 things helped solve problem: 1. setting java configuration runtimes to jdk 1.8. 2. VS Code set to JDK 11 and JAVA_HOME set to jdk 11. 3. As workaround, I created new java project and copied my java classes in src files and build path errors are resolved. There was problem in project imported finally but workaround let me navigate through it. @LeandroArruda Thank you so much. You made my day. – Avneet May 02 '21 at 18:51

2 Answers2

2

I did make a simple test, I did choose one of the most popular extensions package for that and did make the settings indicated.

I'm using RedHat Java Extension, but installed with Microsoft Java Extension Pack: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack, that install RedHat Extension.

In my computer, I had only JDK 8. I'm using linux, I did install JDK simply put in some place, like /usr/lib/jvm, and using update-alternatives to manager the versions, in case I need more than one version.

The documentation of RedHat Extension states that is needed JDK 11 or newer: https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#java.configuration.runtimes.

So, I need to download JDK, I choose Oracle Version, but could be OpenJDK, and I nether needed to configue another version, I just put in some place, /usr/lib/jvm, just for organization, and I did define what was needed at VS Code User Settings: Ctrl + Shift + P, to open Pallete, and enter with Preferences: Open Settings (JSON).

"java.home": "/usr/lib/jvm/jdk-11.0.11",
"java.configuration.runtimes": [
    {
        "name": "JavaSE-1.8",
        "path": "/usr/lib/jvm/jdk1.8.0_281",
    }
]

So I have only Java 8, but for the extension needs I did point path to JAVA_HOME, that is "java.home" setting, to JDK 11 Path and setting up the runtime environment with JDK 8.

With this you can try and verify if the bytecode is generated properly with Java defined at "java.configuration.runtimes" setting, that is JDK 8.

Leandro Arruda
  • 466
  • 6
  • 16
  • @Avneet, I made an update, the configuration will not change, but on really I installed not only RedHat Java Extension, but the MIcrosoft Java Extension Package that includes RedHat Extension. Thus, you can run the project clicking with right button in Main class and have more ease resource to setting up, as test routines and so on. – Leandro Arruda May 02 '21 at 17:17
  • My package is microsoft java extension package. "java.home": "C:\\Program Files\\Java\\jdk-11.0.6", "java.configuration.runtimes": [ { "name": "JavaSE-1.8", "path": "C:\\Program Files\\Java\\jdk1.8.0_291", "default": true } ] – Avneet May 02 '21 at 17:48
0

There are two possibilities, the JDK or the .classpath.

Check for JDK.

Since vscode-java 1.2.0, JDK 17 is required.

"Since vscode-java 1.2.0, it publishes platform specific versions to Microsoft VS Code marketplace. The platform versions have JRE 17 embedded in Java extension for platforms such as win32-x64, linux-x64, linux-arm64, darwin-x64, darwin-arm64." Source

Open settings.json. Can open the settings by ctrl+shift+P and type settings.json. Check for the java.jdt.ls.java.home key since java.home is already deprecated. Set the value to your JDK 17 path.

"java.jdt.ls.java.home":"c:\\Program Files\\Eclipse Adoptium\\jdk-17.0.8.7-hotspot",

Don't worry about the java version that you need to use for the project, can set the version by specify java.configuration.runtimes. Like example,

"java.configuration.runtimes": [
        {
            "name": "JavaSE-1.8",
            "path": "C:\\Program Files\\Java\\jdk1.8.0_162"
        },
        {
            "name": "JavaSE-1.6",
            "path": "C:\\Program Files\\Java\\jdk1.6.0_45",
            "default": true
        },
        {
            "name": "JavaSE-1.7",
            "path": "C:\\Program Files\\Eclipse Adoptium\\jdk-17.0.8.7-hotspot"
        }
    ]
Check for .classpath

Open .classpath.

  • Check the container path. Can search for this keyword kind="con".
  • Make sure the name for the last value is same as the value set in java.configuration.runtimes.names.
  • For example I am using JavaSE-1.6, the last value container path should be JavaSE-1.6. So it should be, <classpathentry kind="con"path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclise.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>

Hopefully it helps.

Ras
  • 159
  • 2
  • 5