Comments (4)
@mklinkj Wow, I really appreciate the detailed solution. And apologies for the typo.
Thanks to your example, I've managed to find the source of the problem. There appeared to be two separate problems, which affected the "Language Support for Java" and "Gradle for Java" extensions separately.
I do have the following setting in my build.gradle
file, which I forgot to mention. It seems like you've mentioned it as well.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(18)
}
}
Here are the two problems (not bugs but user errors) I found:
- JavaSE-18 was required in
build.gradle
, but it was unavailable due to it not being listed injava.configuration.runtimes
insettings.json
.- Effect: Language Support for Java imports the wrong JVM (automatically downloaded by Gradle).
- Fix: List it in
java.configuration.runtimes
or remove the requirement inbuild.gradle
- Gradle for Java is not affected.
java.jdt.ls.java.home
does not point to the correct JDK (JavaSE-18).- Effects: Gradle for Java imports the wrong JVM (automatically downloaded by Gradle).
- Fix: Make
java.jdt.ls.java.home
point to the correct JDK or remove the requirement inbuild.gradle
- Language Support for Java is not affected (still manages to find the correct version).
I found that setting java.import.gradle.java.home
or modifying the gradle.properties
file didn't seem to have any effect. I thought setting the gradle.properties
file would help in case 2, but it didn't.
Gradle properties additions I used:
org.gradle.java.installations.auto-detect=false
org.gradle.java.installations.auto-download=false
org.gradle.java.installations.paths=C:/JDK/8,C:/JDK/11,C:/JDK/17,C:/JDK/18,C:/JDK/21
Other notes:
I got link errors, but that was because I clicked the wrong button on the Gradle for Java sidebar (build instead of application/run).
Thanks again for your help!
from vscode-java.
hello.
I created an example test project and checked its operation...
If you want to use the coursework project with JDK 18, you can do it as follows.
{
"java.configuration.runtimes": [
{
"name": "JavaSE-18", // 💡Install Java 18 on the system and add the path
"path": "/home/username/.jdks/openjdk-18",
},
{
"name": "JavaSE-19",
"path": "/home/username/.jdks/openjdk-19",
},
{
"name": "JavaSE-20", // 💡Renamed to JavaSE-20.
"path": "/home/username/.jdks/openjdk-20",
},
{
"name": "JavaSE-22", // 💡Renamed to JavaSE-22.
"path": "/home/username/.jdks/openjdk-22",
"default": true
},
],
// 💡 Path to Java Home to run Gradle for Java
"java.jdt.ls.java.home": "/home/username/.jdks/openjdk-19"
}
TODO:
- Add JavaSE 18 installation path to
java.configuration.runtimes
. - Correctly corrected duplicate
JavaSE-??
name.
"java.jdt.ls.java.home"
is simply the path to run the Gradle for Java extension, and if JAVA_HOME is set globally on the system, it does not seem to need to be specifically specified.
Example Test
Example Project
This is a simple example program that prints the currently running Java Home.
System environment
In my case, to prevent gradle from automatically downloading a JDK that cannot be used, I disabled automatic downloading and manually specified the JDK path.
%USERPROFILE%/.gradle/gradle.properties
org.gradle.java.installations.auto-detect=false
org.gradle.java.installations.auto-download=false
org.gradle.java.installations.paths=C:/JDK/8,C:/JDK/11,C:/JDK/17,C:/JDK/18,C:/JDK/21
VSCode - User Settings.json
"java.configuration.runtimes": [
{
"name": "JavaSE-17",
"path": "C:\\JDK\\17\\",
"default": true
},
{
"name": "JavaSE-21",
"path": "C:\\JDK\\21\\"
},
{
"name": "JavaSE-11",
"path": "C:\\JDK\\11\\"
},
{
"name": "JavaSE-18",
"path": "C:\\JDK\\18\\"
},
{
"name": "JavaSE-1.8",
"path": "C:\\JDK\\8\\"
}
],
// Java runtime path to run Gradle for Java
// Required after installing "Gradle for Java". If the JAVA_HOME environment variable is set globally on the system, it does not need to be set.
"java.import.gradle.java.home": "C:\\JDK\\21",
Java Version settings for example project - build.gradle
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(18)
}
}
Run it by clicking the Run link that appears above the main() method.
If you run the test project with the settings as above...
C:\git-mklinkj\QnA\vscode-java\gradle-java-home-test> cmd /C "C:\JDK\18\bin\java.exe @%LOCALAPPDATA%\Temp\cp_bcmlni503byv5p5eyxxi4hqsu.argfile org.example.App "
Java Home: C:\JDK\18
Run it by clicking application > run Task in “Gradle for Java”
⚠️ Caution
- When changing the JDK version of a project, it is better to use Clean Java Language Server Workspace.
💡 If you have a development environment that uses multiple versions of the JDK environment, I personally think it would be better to disable Gralde's automatic JDK detection function and automatic download function... 😅
Then, please take note of this. Have a great day.
from vscode-java.
I did this because I didn't want Gradle to automatically download and use JDKs that weren't on my system and preferred to use JDKs that I had manually installed on my system.
Glad it worked out for you and thanks for the answer. Have a nice day 👍
There are parts where I gave speculative answers. 😅
// Specifies the folder path to the JDK (17 or more recent) used to launch the Java Language Server.
// This setting will replace the Java extension's embedded JRE to start the Java Language Server.
"java.jdt.ls.java.home": "C:\\JDK\\17",
// The location to the JVM used to run the Gradle daemon.
"java.import.gradle.java.home": "C:\\JDK\\17",
- If
java.jdt.ls.java.home
is not set, Java Language Server runs as the following built-in JRE.%USERPROFILE%\.vscode\extensions\redhat.java-1.30.0-win32-x64\jre\17.0.10-win32-x86_64
- 💡However, when using this value without setting it, there were rare cases where automatic completion of Java basic classes did not work properly.
java.import.gradle.java.home
seems to mean the Java path that Gradle For Java uses when running the Gradle daemon rather than executing Gradle For Java itself.
from vscode-java.
Glad you were able to resolve this. Closing for now but feel free to re-open if there's some other problems.
Note, as of 1.23.0 (we're about to release 1.31.0 soon), JDK detection is done automatically which should lessen the strain of users needing to populate java.configuration.runtimes
manually. See https://www.npmjs.com/package/jdk-utils#usage for some of the supported locations. Also as was pointed out the java.jdt.ls.java.home
& java.import.gradle.java.home
are meant for the JRE used for the JDT-LS/Gradle runtimes. For configuring which JDK is used by an open project, there is java.configuration.runtimes
.
from vscode-java.
Related Issues (20)
- Static import quick fix offered on unrelated error HOT 1
- Base class methods are missing when generating override methods with source action. HOT 3
- Preview features enabled at an invalid source release level 21, preview can be enabled only at source level 22 HOT 2
- JDK config in both windows and linux HOT 8
- Extension download and install failing with msg "corrupt zip". HOT 2
- Import from code actions doesn't work well after delete java file which is created from code actions
- Cursor jumps when typing HOT 1
- ISSUE: Language Support for Java doesn't work properly, it shows unnecessary error underlines HOT 3
- Maven multi-module project mapstruct duplicate generation
- Spring Boot Tools extension may conflict with Java extension HOT 5
- "Go to symbol" should prioritize local classes HOT 1
- Reloading a window apprently clears Java project settings due to different workspace storage chosen
- [vscode devcontainer] Language Support for Java (Syntax Server) client: couldn't create connection to server.
- just lint error HOT 2
- I think this is a wrong warning message
- inform/detect which operating system a jdk comes from and stop this error HOT 1
- Classes often won't show up when typing, but complete just fine in an import statement. HOT 3
- Change color of methods without reference HOT 1
- Android gradle project do not work - Eclipse build.gradle script
- lineSplit not working HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vscode-java.