If this Bazel Java extension managed to correctly configure your workspace, then:
- The Explorer view will have a Java Projects tab
- The Java Projects will contain a Bazel dependencies in addition to the JRE System Library
- The Java Projects will NOT contain Referenced Libraries after the JRE System Library
- Run (Alt/Cmd+Shift+P) the Java: Configure Java Runtime command; the Type should be Unmanaged folder, NOT Maven or Gradle.
- Run the Java: Configure Classpath command; the Sources should include the correct paths
Note that VSC only starts to initialize things when you open the first .java
file/s.
So it's normal in VSC for e.g. Sources to get "gradually initialized" as you open more Java files.
Please note that this extension's is currently still in Preview.
The last Release Version may not include the latest required bug fixes.
Remember to click Switch to Pre-Release Version on the extension installation page!
When the status bar shows {·} Java or Java X.java
editor is open, then click on that to make VSC switch to
Standard Java Mode.
We recommend to add "java.server.launchMode": "Standard"
to your repo's .vscode/settings.json
.
This extension can well in a VS Code Web and/or Remote Development set-up.
If it doesn't seem to work, check the Extension's Runtime Status tag UI; if that shows Runtime Status: Not yet activated. then you have be facing some permission (?) problems in your Web browser (Brave? Firefox?) and can try with another one (Chrome).
Issue #94 has some related background.
- Close (all of) your VSC windows
killall java
(check thatjps
only shows Jps itself)cd YOURPROJECT
bazel build //...
must first work, obviously (double check!)code .
- Double check that the the extension is installed and not disabled
- Run (Alt/Cmd+Shift+P) the Java: Clean Java Language Server Workspace command (this clears the Logs, see below)
- Open (Ctrl-P) any
X.java
file in the editor (this is required to trigger the extension to kick in, see above) - Run the Java: Show Build Job Status command, and wait for it to "quiet down" and everything in it to be
[Done]
- Run the Java: Synchronize Projects with Bazel Project View command
- Run the Java: Refresh Classpath from Bazel BUILD file command
You could also try to uninstall and reinstall the extension, to see if that helps.
Check out the following places for log-like details to spot any problems:
-
As always, check the Problems view; NB you can Filter it for Bazel.
-
Run the Java: Open All Log Files command to open the
.../redhat.java/jdt_ws/.metadata/.log
. -
The command above should also have opened the
client.log.YYYY-MM-DD
Please attach (or copy/paste) all x3 if you file issues for support.
(Rename .log
to e.g. log.txt
and client.log.YYYY-MM-DD
to e.g. client.log.YYYY-MM-DD.json
in order to be able to upload as attachment to GitHub issues.)
The Bazel and JDT Job status is not in these log files, but visible on the Terminal view Bazel Build Status and Java Build Status tabs; if in doubt, have a look at that as well (see also issue #100).
There is a known limitation during initialization of the Java Language Server.
While the initialization is ongoing the Bazel Build Status will remain empty.
It only starts working after initialization completed.
To work around this limitation you should configure a static port for the extension to use.
This can be done by adding -Djava.bazel.staticProcessStreamSocket=22222
to "java.jdt.ls.vmargs":
option in .vscode/settings.json
.
Please note that this Bazel for Java extension (sfdc.bazel-vscode-java
, which adds support for Bazel's Java rules to VSC), is technically completely independent of the VSC Bazel extension (BazelBuild.vscode-bazel
, which adds generic support for Bazel BUILD
files editing, and "externally running" any Bazel targets; but specific nothing for Java).
It's therefore totally possible to run the former (this) without the latter.
When troubleshooting, it can sometimes be slightly confusing which extension issues what message. For example, notification pop ups with Command failed: bazel (...)
errors are from the other extension, not this one.
If in doubt, it is therefore recommended to temporarily Disable in Workspace that other Bazel extension when debugging problems with this extension.
!ENTRY org.eclipse.jdt.ls.core 4 0 2024-01-05 12:39:25.798
!MESSAGE Error occured while building workspace. Details:
message: Preview features enabled at an invalid source release level 11, preview can be enabled only at source level 21; code: 2098258; resource: /home/vorburger/git/github.com/vorburger/LearningBazel/java-one/src/main/java/ch/vorburger/learningbazel/Main.java;
This error (visible in .log
) means that you need to use Java 21 to use this extension.
More background in issue #85 and issue #74.
If the Synchronize Projects with Bazel View and Refresh Classpath from Bazel BUILD file menus are not available on right-click on Folder, then the extension is not correct installed, or has been manually disabled on the workspace.
Error (...) Unable to detect Bazel version of binary 'bazel'!
Cannot run program "bazel": error=2, No such file or directory
java.io.IOException: Cannot run program "bazel": error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at com.salesforce.bazel.sdk.command.BazelBinaryVersionDetector.detectVersion(BazelBinaryVersionDetector.java:57)
at com.salesforce.bazel.eclipse.core.extensions.DetectBazelVersionAndSetBinaryJob.run(DetectBazelVersionAndSetBinaryJob.java:52)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.io.IOException: error=2, No such file or directory
The extension attempts to launch bazel
, but it's not on your $PATH
.
If you use Bazelisk (which we recommend!),
make sure you have a symlink from bazel
to bazelisk
on $PATH
. On macOS,
Homebrew sets this up. On Linux, you need to do this yourself e.g. in your dotfiles.
Alternatively, add bazel_binary: bazelisk
in the project's .bazelproject
configuration file (but see issue #88).
More background in issue #477.
If there is a Build tool conflicts are detected in workspace. Which one would you like to use? Maven, or Gradle? pop-up:
Just click Maven - and this extension will still correctly kick-in and initialize classpaths and source folders etc. from Bazel.
We recommend to add the following to your repo's .vscode/settings.json
if you run into this problem:
"java.import.bazel.enabled": true,
"java.import.maven.enabled": false,
"java.import.gradle.enabled": false,
More background in issue #82.
To narrow down root causes of problems, it may be interesting to try opening the same project using the Bazel Eclipse Feature and see if that works.