Bazel 构建 java 演示:构建正常但无法运行
Posted
技术标签:
【中文标题】Bazel 构建 java 演示:构建正常但无法运行【英文标题】:Bazel build java demo: build ok but fail to run 【发布时间】:2019-06-28 19:15:44 【问题描述】:我是 bazel 的新手,并且有这个演示项目:
(1)mkdir 演示项目
(2)cd 演示项目
(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 构建 //:你好
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
一切似乎都很好,但是当我尝试时
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
如上所述,两个命令都不成功。那么bazel编译后,如何运行java可执行文件呢?
【问题讨论】:
在第 6 步中,在定义main_class
之后是一个额外的逗号吗? (需要吗?还是只是问题上的错字?)
我只是试图删除它,然后同样的问题。谢谢。
你需要运行 bazel-bin/hello
或 java -cp bazel-bin/hello.jar com.demo.DemoRunner
,即使你可以使用 bazel run //:hello
运行它
感谢 Bhavik,这对我有用
【参考方案1】:
Bazel 命令run
可用于运行指定目标(另请参阅 Bhavik 的评论):
bazel run //:hello
【讨论】:
以上是关于Bazel 构建 java 演示:构建正常但无法运行的主要内容,如果未能解决你的问题,请参考以下文章