Unable to use java_binary generated wrapper script without runfiles and only a _deploy.jar #25092
Labels
P3
We're not considering working on this, but happy to review a PR. (No assignee)
team-Rules-Java
Issues for Java rules
type: feature request
Description of the bug:
While java_binary creates an executable wrapper script, it is not runnable without the target runfiles being present, even if the _deploy.jar is present.
According to the documentation: https://bazel.build/reference/be/java#java_binary:
"Building the _deploy.jar target for your rule creates a self-contained jar file with a manifest that allows it to be run with the java -jar command or with the wrapper script's --singlejar option."
The problem is that the %needs_runfiles% substitution overwrites the --singlejar option.
As seen here: https://github.com/bazelbuild/rules_java/blob/master/java/bazel/rules/bazel_java_binary.bzl#L251, the toggle for the substitution is for the java runtime toolchain to exist at an absolute path. This would imply the java_binary must be built while referring to a static local JDK. While that makes sense for the
bazel run
use case, it ignores the --singlejar use case when used with a JAVABIN override.Additionally, specifying --nobuild_runfile_links renders the normal
bazel run
use case inoperable for the wrapper script, as there are no runfiles generated at all.I was unable to find the original reasoning for using the java runtime absolute path as the toggle for the runfiles substitution, but it seems like either the use case for the _deploy.jar with the wrapper script needs to be updated in the documentation to clarify that runfiles are always required or the substitutions in the script template updated to support the use case of a standalone _deploy.jar and the wrapper script.
Which category does this issue belong to?
Java Rules
What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
git clone https://github.com/bazelbuild/examples
cd examples/java-tutorial
echo "7.4.1" > .bazelversion
export JAVA_HOME=/path/to/local/JDK/21
(openjdk 21.0.3.0.101 2024-07-16 LTS was used).bazel build --nobuild_runfile_links :ProjectRunner :ProjectRunner_deploy.jar
bazel-bin/ProjectRunner --singlejar
bazel-bin/ProjectRunner: Cannot locate runfiles directory. (Set $JAVA_RUNFILES to inhibit searching.)
bazel-bin/ProjectRunner
bazel-bin/ProjectRunner: Cannot locate runfiles directory. (Set $JAVA_RUNFILES to inhibit searching.)
Which operating system are you running Bazel on?
OSX 14.7.2
What is the output of
bazel info release
?release 7.4.1
If
bazel info release
returnsdevelopment version
or(@non-git)
, tell us how you built Bazel.No response
What's the output of
git remote get-url origin; git rev-parse HEAD
?If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.
No response
Have you found anything relevant by searching the web?
This issue was reported previously here, but closed as stale: #10373
Any other information, logs, or outputs that you want to share?
No response
The text was updated successfully, but these errors were encountered: