Vscode-java: How can I set the Java runtime version to JDK 8 for a specific project?

Created on 31 Jul 2020  路  8Comments  路  Source: redhat-developer/vscode-java

[provide a description of the issue]

Environment
  • Operating System: macOS Catalina 10.15.6
  • JDK version: 14.0.2 but I want to run my program with 1.8.0_221
  • Visual Studio Code version: 1.47.3
  • Java extension version: 0.65.0
Steps To Reproduce
  1. [step 1]
  2. [step 2]

[Please attach a sample project reproducing the error]
[Please attach logs](https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting#enable-logging)

Current Result
Expected Result
Additional Informations

I was wondering if there was a way to make a single project run java programs using JDK 8. I have already put this specific JDK in java.configuration.runtimes but I am not seeing the status bar icon to change the Java Language Level. I have gotten it to work by setting default to true for the JDK I want to use in java.configuraiton.runtimes but I was wondering if there was a way to do this that didn't involve changing my settings for all projects.

Most helpful comment

Would it be possible to make the java.configuration.runtimes setting overridable on a per workspace basis ? It would indeed be useful for a "no build tool" setup.
Right now I don't really understand how it can be useful to register multiple runtimes in the User settings if only the default one can be used.

All 8 comments

I guess you're not using any build tool. For the standalone Java files, the extension will use the language level from the default JDK runtime you configured in user setting.

If you use maven or gradle, their build file such as pom.xml/build.gradle provides options to specify the complier version.

So if I don't use a build tool I have to change the runtime in settings?

yes, you can only change the global user setting for that.

Thanks

Would it be possible to make the java.configuration.runtimes setting overridable on a per workspace basis ? It would indeed be useful for a "no build tool" setup.
Right now I don't really understand how it can be useful to register multiple runtimes in the User settings if only the default one can be used.

@n-peugnet what sorts of projects are you using with vscode-java?

@fbricon I would say any kind of project. For bigger projects and especially if I need to work with other people I usually setup Maven to have a common build system. But for smaller/school project I would love to be able to just make a folder and set all the build configuration in my .vscode/settings.json, or maybe even better, using a dedicated config file in .vscode containing the java project configuration as vscode-cpptools is doing for example. This also applies to https://github.com/redhat-developer/vscode-java/issues/1615 and to the source folders as well as for now it seems that it is only configurable by right-click -> Add folder to Java source path (I haven't already looked if there is an issue for this).

For the current issue, I think that it would be sufficient to have a java.project.runtime in .vscode/settings.json which allows to select one of the runtimes defined in the User settings based on its name property.

And to clarify my current use case, I am currently doing school projects using Hadoop. The latest stable version 3.2.1 does not support Java11 so I have to use an older version (1.8). I am using vscode to code and compile some MapReduce jobs but these have to be compiled with 1.8 for yarn to be able to run them. I use Java11 in almost every other projects so I would like not to have to switch the default version in my User settings each time I switch projects. Maven would be overkill and since I am already setting the referencedLibraries in vscode to get autocompletion, it is very easy to just use the class compiled by vscode-java to run the jobs.

Another solution would be to be able to set the -target option of javac to compile 1.8 compliant classes (or maybe add a compilerOptions setting allowing a broader range of configuration) but as the runtime configuration already exists I thought that it would be easier to implement.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roben picture roben  路  4Comments

jcjolley picture jcjolley  路  3Comments

danielcalvogonzalez picture danielcalvogonzalez  路  3Comments

shawmanz32na picture shawmanz32na  路  3Comments

ViniciusAtaide picture ViniciusAtaide  路  4Comments