Hind Forsum
Hind Forsum

Reputation: 10507

Bazel build java demo: build ok but fail to run

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?

Upvotes: 2

Views: 739

Answers (2)

SG_Bazel
SG_Bazel

Reputation: 355

bazel build //... & bazel run //... builds and executes everything. You can find the build specific to the targets from here

//... All targets in packages in the workspace.

Upvotes: 0

Vertexwahn
Vertexwahn

Reputation: 8142

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

bazel run //:hello

Upvotes: 1

Related Questions