Search code examples
javamacosbazel

bazel run *_deploy.jar fails with "cannot execute binary file"


Given the following BUILD file

java_library(
  name='target_lib',
  srcs=['target.java',],
)

java_binary(
  name='target',
  runtime_deps=[':target_lib',],
  main_class='com.example.target',
)

I can perform a bazel run //path:target on a java_binary and it successfully executes my application; however when attempting to execute bazel run //path:target_deploy.jar it failes with "cannot execute binary file"

I'm attempting this on MacOS (if that matters)

Also if I manually attempt to do a java -jar bazel-bin/path/target_deploy.jar I find that the manifest doesn't specify a main_class. Unsure if that's related.

Bazel version information:

$ bazel version
Build label: 0.26.1-homebrew
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Jun 6 18:36:29 2019 (1559846189)
Build timestamp: 1559846189
Build timestamp as int: 1559846189

Solution

  • This is working as intended. bazel run doesn't actually execute the deploy jar, but a stub script that sets up the runtime environment for the jar determined by the target's dependency graph. If you pass a file label (e.g. //x/y:deploy.jar) to bazel run, Bazel will attempt to execute it as a standalone binary without java -jar.