How to find files in runfiles dir?

418 views
Skip to first unread message

pa...@lucidchart.com

unread,
Apr 5, 2018, 6:24:03 PM4/5/18
to bazel-discuss
I have some jars that must be loaded with a non-system classloader.

I can add these jars as data to java_binary, but there's no way to know where they will wind up in the JAVA_RUNFILES directory, yets?

It seems they are saved in the directory as the unaliased identify, which can change.

(bind() is used, so that users can either use an external dependency, or provide their own local version.)

I can search through all run files and do content detection but the whole things seems very messy.

If I write

java_binary(
name = "example",
main_class = "Main",
visibility = ["//visibility:public"],
deps = [
":deps",
],
data = [
"//external:myjar"
],
)

Of all the runfiles, how I can I identify //external:myjar ?

Alex Humesky

unread,
Apr 5, 2018, 7:44:08 PM4/5/18
to pa...@lucidchart.com, bazel-discuss
Two solutions come to mind:

1. Use the args attribute to tell the binary where the jars are. Something like this:

JARS = [
    "//external:myjar",
]

java_binary(
  name = "Test",
  srcs = glob(["java/**"]),
  args = ["--jar=$(location %s)" % j for j in JARS],
  data = JARS,
)

This only works if you run the binary through bazel run though, and requires that the binary understands these flags.

2. Create a "jar manifest" using a genrule that contains the paths of the jars you want. The jar manifest will be at a known location so you can read that at runtime. Something like:

JARS = [
    "//external:myjar",
]

java_binary(
  name = "Test",
  srcs = glob(["java/**"]),
  data = [
    ":jar_manifest",
  ] + JARS,
)

genrule(
  name = "gen_jar_manifest",
  outs = ["jar_manifest"],
  srcs = JARS,
  cmd = " && ".join(["echo '$(location %s)' >> $@" % j for j in JARS]),
)

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/ee7668c3-b389-44d6-80ee-5691dcf53a8b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Humesky

unread,
Apr 5, 2018, 7:50:35 PM4/5/18
to pa...@lucidchart.com, bazel-discuss
(You could also get fancy with #2 and instead of generating a file to manually read at runtime, you could generate a java file that has the file paths in a list, and then it's just available as a part of your application, but I don't know if I'd actually recommend this.)

pa...@lucidchart.com

unread,
Apr 6, 2018, 1:37:49 AM4/6/18
to bazel-discuss
Good to know the location trick.

I was using genquery to obtain the bound actual. Location is much better.

ittai zeidman

unread,
Apr 6, 2018, 2:13:37 AM4/6/18
to bazel-discuss
I’m fairly certain what you want is $execpath and not $location since the former is for runtime and the latter is for build times

Marcel Hlopko

unread,
Apr 11, 2018, 2:55:29 AM4/11/18
to ittai zeidman, László Csomor, bazel-discuss

On Fri, Apr 6, 2018 at 8:13 AM ittai zeidman <itt...@gmail.com> wrote:
I’m fairly certain what you want is $execpath and not $location since the former is for runtime and the latter is for build times

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Geschäftsführer: Paul Manicle, Halimah DeLaine Prado | Registergericht und -nummer: Hamburg, HRB 86891
Reply all
Reply to author
Forward
0 new messages