Bazel build java demo: build ok but fail to run

╄→гoц情女王★ 提交于 2019-12-23 22:26:47

问题


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?


回答1:


The Bazel command run can be used to run specified targets (see also comment from Bhavik):

bazel run //:hello


来源:https://stackoverflow.com/questions/54526458/bazel-build-java-demo-build-ok-but-fail-to-run

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!