无法在 bazel java 中找到 runfiles 目录

问题描述 投票:0回答:1

我有一个 genrule,它将 java 二进制文件作为 src 并使用 $(location ) 作为 cmd。当执行 genrule 时,我得到

Cannot locate runfiles directory. (Set $JAVA_RUNFILES to inhibit searching.)

如果我先构建二进制文件,然后执行 genrule,它就可以正常工作。我该如何解决这个问题?

bazel bazel-java
1个回答
0
投票

如果将在

java_binary
cmd
属性中调用
genrule
目标,则
java_binary
应位于
tools
genrule
属性中,而不是
srcs
:

BUILD

java_binary(
  name = "Main",
  srcs = ["java/com/Main.java"],
)

genrule(
  name = "gen",
  tools = [":Main"],
  outs = ["out"],
  cmd = "$(location :Main) > $@",
)

Main.java

package com;

public class Main {
  public static void main(String[] args) {
    System.out.println("hello world");
  }
}
$ bazel build out
INFO: Analyzed target //:out (1 packages loaded, 4 targets configured).
INFO: Found 1 target...
Target //:out up-to-date:
  bazel-bin/out
INFO: Elapsed time: 0.297s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

$ cat bazel-bin/out
hello world
© www.soinside.com 2019 - 2024. All rights reserved.