Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • B bazel-java
  • Project information
    • Project information
    • Activity
    • Labels
    • Planning hierarchy
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Service Desk
    • Milestones
  • Jira
    • Jira
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Administrator
  • bazel-java
  • Wiki
  • 3. Hello World in Java using Bazel

Last edited by AzbotGithub Jan 06, 2022
Page history

3. Hello World in Java using Bazel

Installing Bazel

This page describes the various platforms supported by Bazel and links to the packages for more details.

The Bazel team officially supports Bazel on these platforms:

  • Ubuntu Linux
  • macOS
  • Windows

Install the JDK

  1. Install Java JDK (preferred version is 11, however versions between 8 and 15 are supported).

  2. Set the JAVA_HOME environment variable to point to the JDK.

    • On Linux/macOS:

      export JAVA_HOME="$(dirname $(dirname $(realpath $(which javac))))"
    • On Windows:

      1. Open Control Panel.
      2. Go to "System and Security" > "System" > "Advanced System Settings" > "Advanced" tab > "Environment Variables..." .
      3. Under the "User variables" list (the one on the top), click "New...".
      4. In the "Variable name" field, enter JAVA_HOME.
      5. Click "Browse Directory...".
      6. Navigate to the JDK directory (for example C:\Program Files\Java\jdk1.8.0_152).
      7. Click "OK" on all dialog windows.

Clone Bazel-java

Retrieve the project from Evydtech repository:

git clone https://github.com/evydtech/bazel-java.git

The sample project for this tutorial is in the examples/java-tutorial directory and is structured as follows:

java-tutorial
├── BUILD
├── src
│   └── main
│       └── java
│           └── com
│               └── example
│                   ├── cmdline
│                   │   ├── BUILD
│                   │   └── Runner.java
│                   ├── Greeting.java
│                   └── ProjectRunner.java
└── WORKSPACE

Build with Bazel

Set up the workspace

Before you can build a project, you need to set up its workspace. A workspace is a directory that holds your project's source files and Bazel's build outputs. It also contains files that Bazel recognizes as special:

  • The WORKSPACE file, which identifies the directory and its contents as a Bazel workspace and lives at the root of the project's directory structure,

  • One or more BUILD files, which tell Bazel how to build different parts of the project. (A directory within the workspace that contains a BUILD file is a package. You will learn about packages later in this tutorial.)

To designate a directory as a Bazel workspace, create an empty file named WORKSPACE in that directory.

When Bazel builds the project, all inputs and dependencies must be in the same workspace. Files residing in different workspaces are independent of one another unless linked, which is beyond the scope of this tutorial.

Understand the BUILD file

A BUILD file contains several different types of instructions for Bazel. The most important type is the build rule, which tells Bazel how to build the desired outputs, such as executable binaries or libraries. Each instance of a build rule in the BUILD file is called a target and points to a specific set of source files and dependencies. A target can also point to other targets.

Take a look at the java-tutorial/BUILD file:

java_binary(
    name = "ProjectRunner",
    srcs = glob(["src/main/java/com/example/*.java"]),
)

In our example, the ProjectRunner target instantiates Bazel's built-in java_binary rule. The rule tells Bazel to build a .jar file and a wrapper shell script (both named after the target).

The attributes in the target explicitly state its dependencies and options. While the name attribute is mandatory, many are optional. For example, in the ProjectRunner rule target, name is the name of the target, srcs specifies the source files that Bazel uses to build the target, and main_class specifies the class that contains the main method. (You may have noticed that our example uses glob to pass a set of source files to Bazel instead of listing them one by one.)

Build the project

To build your sample project, navigate to the java-tutorial directory and run:

bazel build //:ProjectRunner

In the target label, the // part is the location of the BUILD file relative to the root of the workspace (in this case, the root itself), and ProjectRunner is the target name in the BUILD file. (You will learn about target labels in more detail at the end of this tutorial.)

Bazel produces output similar to the following:

   INFO: Found 1 target...
   Target //:ProjectRunner up-to-date:
      bazel-bin/ProjectRunner.jar
      bazel-bin/ProjectRunner
   INFO: Elapsed time: 1.021s, Critical Path: 0.83s

Congratulations, you just built your first Bazel target! Bazel places build outputs in the bazel-bin directory at the root of the workspace. Browse through its contents to get an idea for Bazel's output structure.

Now test your freshly built binary:

bazel-bin/ProjectRunner

Happy building!

Clone repository
  • 1. DevOps
  • 2. Monorepos & Bazel
  • 3. Hello World in Java using Bazel
  • 4. Infrastructure Deployment
  • Dev Infrastructure
  • Home
  • Log management