Bazel build java demo: build ok but fail to run

2019-07-21 01:53发布

问题:

I'm new to bazel and have this demo project:

(1)mkdir demo-project

(2)cd demo-project

(3)mkdir -p src/main/java/com/demo

(4)vi src/main/java/com/demo/DemoRunner.java

package com.demo;
public class DemoRunner {
    public static void main(String args[]) {
        Hello.hello();
    }
}

(5)vi src/main/java/com/demo/Hello.java

package com.demo;

public class Hello {
    public static void hello() {
        System.out.println("hello,world");
    }
}

(6)vi ~/demo-project/BUILD

java_binary(
    name = "hello",
    srcs = glob(["**/*.java"]),
    main_class = "com.demo.DemoRunner",
)

(7) bazel build //:hello

Starting local Bazel server and connecting to it...
...........
Analyzing: target //:hello (2 packages loaded)
INFO: Analysed target //:hello (15 packages loaded).
INFO: Found 1 target...
Target //:hello up-to-date:
bazel-bin/hello.jar
bazel-bin/hello
INFO: Elapsed time: 60.505s, Critical Path: 1.24s
INFO: 1 process: 1 worker.
INFO: Build completed successfully, 6 total actions

Everything seems OK, but when I tried to

java bazel-bin/hello # Cannot find or load main class bazel-bin.hello
java -jar bazel-bin/hello.jar # Cannot find main list property in bazel-bin/hello.jar

Neither command is successful, as described above. So after bazel compile, how can I run the java executable?

Thanks a lot.