eclipse-jdtls / eclipse.jdt.ls

Java language server
1.76k stars 394 forks source link

[Java 11] jdt.ls is reporting errors when project relies on --add-exports #1492

Open mpsq opened 4 years ago

mpsq commented 4 years ago

The code in my project relies on some classes from jdk.internal. To make it compile without issues, maven passes --add-exports java.base/jdk.internal.math=ALL-UNNAMED to javac. I use jdt.ls via lsp-java for Emacs, lsp-java is passing the arguments correctly since the server is launched with the parameters:

/usr/bin/java -Declipse.application=org.eclipse.jdt.ls.core.id1 -Dosgi.bundles.defaultStartLevel=4 -Declipse.product=org.eclipse.jdt.ls.core.product -Dlog.protocol=true -Dlog.level=ALL --add-exports java.base/jdk.internal.math=ALL-UNNAMED -noverify -Xmx1G -XX:+UseG1GC -XX:+UseStringDeduplication -jar /home/mpsq /.emacs.d/.cache/lsp/eclipse.jdt.ls/plugins/org.eclipse.equinox.launcher_1.5.700.v20200207-2156.jar -configuration /home/mpsq/.emacs.d/.cache/lsp/eclipse.jdt.ls/config_linux -data /home/mpsq/.emacs.d/workspace/ --add-modules=ALL-SYSTEM --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED

However, jdt.ls is still reporting errors and fails to compile the project because of jdk.internal.math not being accessible.

Details available here: https://github.com/emacs-lsp/lsp-java/issues/237

Let me know if I am missing something or if there is anything I can do to help.

paulmedynski-microsoft commented 6 months ago

I have the same issue when using VS Code and the Language Support for Java extension, that internally uses jdt.ls:

NOTE: Picked up JDK_JAVA_OPTIONS: --add-exports java.xml/com.sun.org.apache.xerces.internal.impl.dtd=ALL-UNNAMED --add-exports java.xml/com.sun.org.apache.xerces.internal.xni.parser=ALL-UNNAMED --add-exports java.xml/com.sun.org.apache.xerces.internal.xni.grammars=ALL-UNNAMED --add-exports java.xml/com.sun.org.apache.xerces.internal.util=ALL-UNNAMED --add-exports java.xml/com.sun.org.apache.xerces.internal.parsers=ALL-UNNAMED --add-exports java.xml/com.sun.org.apache.xerces.internal.jaxp=ALL-UNNAMED
...
[Error - 11:45:20 AM] Apr 3, 2024, 11:45:20 AM Error occured while building workspace. Details: 
 message: The import com.sun.org.apache.xerces cannot be resolved; code: 268435846; resource <redacted>
...

Can anyone from this project comment? Thanks!

paulmedynski-microsoft commented 5 months ago

The VSCode Language Support for Java extension is now passing the --add-exports correctly when starting the JVM for JDT LS:

https://github.com/redhat-developer/vscode-java/issues/3577

But I still get errors from the language server about unresolved imports:

[Error - 2:34:52 PM] Apr 18, 2024, 2:34:52 PM Error occured while building workspace. Details: 
 message: The import com.sun.org.apache.xerces cannot be resolved; code: 268435846; resource <redacted>

Thoughts? Comments?

snjeza commented 5 months ago

@paulmedynski-microsoft Could you, please, attach a project example?

paulmedynski-microsoft commented 5 months ago

Sure thing. I'll get something setup today. Thanks for replying.

paulmedynski-microsoft commented 5 months ago

After building a vanilla sample project, it turns out that the JDT errors were caused by a parent POM setting maven.compiler.release, which in turn sets the javac --release argument. This is incompatible with --add-exports, so explicitly unsetting the release value fixes things for me.

See: https://stackoverflow.com/questions/71019097/clear-unset-a-system-property-maven-compiler-release-so-that-is-is-no-longer-pre

I consider the issue fixed for me, but I'm using JDK 21, so I'm not sure if this solution would work for the original poster using JDK 11.