vscode-java: The type javax.annotation.Nonnull cannot be resolved

In one of my projects, I’m getting an error after upgrading to the latest version:

The type javax.annotation.Nonnull cannot be resolved. It is indirectly referenced from required .class filesJava(16777540)
Annotation type 'javax.annotation.Nonnull' cannot be found on the build path, which is implicitly needed for null analysisJava(536871894)

I saw the changelog and found that the issue relates to enhancement - Enable annotation-based null analysis. See #1693.

Removing “javax.annotation.Nonnull” from “java.compile.nullAnalysis.nonnull” config fixes the problem for me.

Environment
  • Operating System: MacOS 12.6
  • JDK version: openjdk 17 2021-09-14
  • Visual Studio Code version: 1.71.2
  • Java extension version: 1.11.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 16 (9 by maintainers)

Most upvoted comments

You should be able to disable this feature with the following settings (on the individual workspace, or globally).

.vscode/settings.json

{
    "java.compile.nullAnalysis.nonnull": [
    ],
    "java.compile.nullAnalysis.nullable": [
    ]
}

@yotov thank-you. Am able to reproduce.

Looking at the classpath of the project, I see both com.google.code.findbugs:jsr305:jar:3.0.2:runtime & org.springframework:spring-core:jar:5.3.22:compile. So basically we have 2 annotation providers on the classpath. It looks like the defaults prefer the jsr305 provides (eg. javax.annotation…) over the spring framework provider (eg. org.springframework.lang…). However clearly in this case it should be reversed.

Can you try, the following setting ? It’s basically just the default settings, but the spring framework annotations are moved to the top.

.vscode/settings.json

{
	"java.compile.nullAnalysis.nullable": [
		"org.springframework.lang.Nullable",
		"javax.annotation.Nullable",
		"org.eclipse.jdt.annotation.Nullable"
	],
	"java.compile.nullAnalysis.nonnull": [
		"org.springframework.lang.NonNull",
		"javax.annotation.Nonnull",
		"org.eclipse.jdt.annotation.NonNull"
	]
}

Does this have anything to do with the issue mentioned above?

@chowethan @yotov Could you try to remove .project, .classpath, .settings from your project?

@jvansant The fix https://github.com/eclipse/eclipse.jdt.ls/pull/2264 is available in the newest pre-release verison 1.12.2022101204, you can try to switch to this version to see if it works.

If anyone is able to point to a sample project (or upload one) that causes this problem, it would be very helpful.

Looking at some past issues in JDT, I noticed this would happen when null analysis was enabled, but no implementation on the classpath. Since we currently enable the analysis automatically, it’s obviously being enabled in circumstances where it shouldn’t be.

@rgrunber here is a sample project that causes the problem for me - https://github.com/yotov/vscode-nullable I managed to find that I get this error in projects that use namedParameterJdbcTemplate.batchUpdate and having a spring-kafka in dependencies. Removing the line with batchUpdate fixes the error. Removing the spring-kafka dependency also fixes the error.

Hope that this can help you 😃

I have setup my settings.json file as instructed (I chose the workspace root .vscode directory)

{
	"java.compile.nullAnalysis.nonnull": [],
	"java.compile.nullAnalysis.nullable": []
}

However, I’m still seeing the same error after running mvn clean install as well as the extension command Java: Clean Java Language Server Workspace

Annotation type ‘javax.annotation.Nonnull’ cannot be found on the build path, which is implicitly needed for null analysis Java(536871894)

Does this have anything to do with the issue mentioned above?