Hind Forsum
Hind Forsum

Reputation: 10507

What is the raw command-line to call a bazel built java program?

bazel built cpp/java programs and run them with a very complex bazel wrapper. I wish I don't rely on this kind of bazel wrapper, because I want a clean runtime/installation package. Here's a simple java example from my "BUILD"

java_binary(
    name = "hello",
    srcs = ["src/main/java/com/demo/DemoRunner.java"],
    main_class = "com.demo.DemoRunner",
    deps = [":HelloTest"],
)

java_library(
    name = "HelloTest",
    srcs = ["src/main/java/com/demo/Hello.java"],
    visibility = ["//src/main/java/com/demo/child:__pkg__"],
)

I can "bazel build hello" and "bazel run hello", no problem. But when I tried:

# java -classpath bazel-bin/hello.jar com.demo.DemoRunner
Exception in thread "main" java.lang.NoClassDefFoundError: com/demo/Hello
    at com.demo.DemoRunner.main(DemoRunner.java:4)
Caused by: java.lang.ClassNotFoundException: com.demo.Hello
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

My requirement is just to run bazel built jar with raw command-line. How to correct my command, I guess I have to set some classpath or environment variables?

Please kindly help. Thanks a lot.

Upvotes: 0

Views: 501

Answers (1)

Jin
Jin

Reputation: 13473

You'll probably want to java -jar the implicit _deploy.jar output.

$ bazel build //:hello_deploy.jar && java -jar bazel-bin/hello_deploy.jar.

Upvotes: 1

Related Questions