0

I once had a problem with Flutter and I had to download jdk. I gave it a path D:/jdk but then i made new partition and downloaded it again on C:\Program Files\Java\jdk-16.0.1. Now when I'm running an app in Visual Studio Code there is an error:

FAILURE: Build failed with an exception.

  • What went wrong: Supplied javaHome must be a valid directory. You supplied: D:\jdk

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

I don't know what do, I already reinstalled gradle, jdk and VSCode. Whjat I have to do to delete this path from VSCode memory?

EDIT: FIXED

So I fixed the problem by deleting temporal files, optimalizing storages and clearing Windows cache (also cache of VSCode). I also restarted my laptop after the process

I answered this question with a link to tutorial how to do it, there are also additional steps like doing it with some program, in my case it wasn't required. Hope it helps someone

Jan Tomczak
  • 13
  • 1
  • 2
  • 6
  • 1
    Without knowing what exactly you configured back then, it may be challenging to find the exact setting to reconfigure. Have you taken a look at [this answer](https://stackoverflow.com/a/21212790/4216641)? Also, try to look whether VS Code has some settings to configure the JDK home for gradle. Maybe it is configured there. – Turing85 May 07 '21 at 19:56
  • 1
    Is this a windows machine? Should probably use either / slashes or \\ for a path – Anthony Cathers May 07 '21 at 20:01
  • so in JAVA_HOME i should use \\? cause previously i was using \ – Jan Tomczak May 07 '21 at 20:09

5 Answers5

0

Open Command Palette--> Java: Configure Java Runtime--> Java Tooling Runtime, if your JDK is installed correctly and JAVA_HOME is added to PATH, Java extension will detect it automatically.

You can choose the one you wanted and set it as JDK for Language Server, which is equivalent to add the following configurations in Settings.json:

"java.home": "C:\\...\\AdoptOpenJDK",

enter image description here

Don't forget to reload window to make the change effective.

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
  • Hi, I can't change the JDK for language server in any way. https://imgur.com/a/vWigcg5 – Jan Tomczak May 09 '21 at 14:34
  • So, this is the one Java extension detected. The one you want to set in your question is in D:/. Please check if you have added the jdk installed in D:/ to PATH. – Molly Wang-MSFT May 10 '21 at 01:10
0

So I fixed the problem by deleting temporal files, optimalizing storages and clearing Windows cache (also cache of VSCode). I also restarted my laptop after the process

[https://www.drivereasy.com/knowledge/how-to-clear-cache-on-windows-10/#b][1]
Jan Tomczak
  • 13
  • 1
  • 2
  • 6
0

I also experienced a confusing issue with a Gradle project I was trying to build in Visual Studio Code. It couldn't find the Gradle dependencies. Thus it couldn't resolve a number of the artifacts (variables, annotations, etc.) that were unrecognized.

To fix this issue, I did the following:

  1. Set the java.jdt.ls.java.home property to reference the Java Home location.
  2. Confirm that you have a Java Runtime of at least Java 11 set (at least for the current plugin requirements)
  3. Installed the "Gradle for Java" extension (VS Code) from Microsoft
  4. Cleared the cache and cleaned the VS Code cache.
  5. Restarted VS Code.

By clearing the VS Code cache it re-initiated Gradle when I re-opened the project, and it was able to resolve the missing artifacts. Those that did not resolve automatically were now recognized through code complete and the contextual tools provided by VS Code and the Gradle plugin.

I would expect the tooling for Gradle would allow a re-load of it's attributes and project references, but a restart did it automatically for me.

Example of Java Settings in settings.json file:

{
  "java.configuration.runtimes": [
      {
        "name": "JavaSE-1.8",
        "path": "C:\\JAVA\\JAVA_1.8_..._x86_64",
      },
      {
        "name": "JavaSE-11",
        "path": "C:\\JAVA\\Java11_..._x86_64"
      }
    ],
    "java.jdt.ls.java.home": "C:\\JAVA\\Java11_..._x86_64"
}

enter image description here

Michael M
  • 1,303
  • 1
  • 12
  • 12
  • How do you "Clear[...] the cache and clean[...] the VS Code cache"? When I search the command palette I see no options for that. – David Jun 09 '22 at 19:56
0

In my case the invalid javaHome was set in the workspace storage. I found the wrong path in one of the directories under the c:\Users\<username>\AppData\Roaming\Code\User\workspaceStorage\ folder. I just simply deleted the whole directory and after it I had to reopen the project folder.

0

For me I solved this issue by editing android/gradle.properties file. I added the following line.

org.gradle.java.home=/Path/To/Your/Working/Jdk/home
Linden X. Quan
  • 584
  • 5
  • 18