CINXE.COM

Bazel Tutorial: Build a Java Project

<!doctype html> <html lang="en" dir="ltr"> <head> <meta name="google-signin-client-id" content="157101835696-ooapojlodmuabs2do2vuhhnf90bccmoi.apps.googleusercontent.com"> <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/developerprofiles https://www.googleapis.com/auth/developerprofiles.award"> <meta property="og:site_name" content="Bazel"> <meta property="og:type" content="website"><meta name="theme-color" content="#0c713a"><meta charset="utf-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="manifest" href="/_pwa/bazel/manifest.json" crossorigin="use-credentials"> <link rel="preconnect" href="//www.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.googleapis.com" crossorigin> <link rel="preconnect" href="//apis.google.com" crossorigin> <link rel="preconnect" href="//www.google-analytics.com" crossorigin><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"> <link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Material+Icons&family=Material+Symbols+Outlined&display=block"><link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/css/app.css"> <link rel="shortcut icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/favicon-prod.png"> <link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/touchicon-180.png"><link rel="canonical" href="https://bazel.build/versions/7.0.0/start/java"><link rel="search" type="application/opensearchdescription+xml" title="Bazel" href="https://bazel.build/s/opensearch.xml"> <link rel="alternate" hreflang="en" href="https://bazel.build/versions/7.0.0/start/java" /><link rel="alternate" hreflang="x-default" href="https://bazel.build/versions/7.0.0/start/java" /><link rel="alternate" hreflang="zh-Hans" href="https://bazel.build/versions/7.0.0/start/java?hl=zh-cn" /><link rel="alternate" hreflang="zh-Hant" href="https://bazel.build/versions/7.0.0/start/java?hl=zh-tw" /><link rel="alternate" hreflang="hi" href="https://bazel.build/versions/7.0.0/start/java?hl=hi" /><link rel="alternate" hreflang="id" href="https://bazel.build/versions/7.0.0/start/java?hl=id" /><link rel="alternate" hreflang="ja" href="https://bazel.build/versions/7.0.0/start/java?hl=ja" /><link rel="alternate" hreflang="ko" href="https://bazel.build/versions/7.0.0/start/java?hl=ko" /><link rel="alternate" hreflang="pt-BR" href="https://bazel.build/versions/7.0.0/start/java?hl=pt-br" /><link rel="alternate" hreflang="es-419" href="https://bazel.build/versions/7.0.0/start/java?hl=es-419" /><link rel="alternate" hreflang="th" href="https://bazel.build/versions/7.0.0/start/java?hl=th" /><link rel="alternate" hreflang="tr" href="https://bazel.build/versions/7.0.0/start/java?hl=tr" /><link rel="alternate" hreflang="vi" href="https://bazel.build/versions/7.0.0/start/java?hl=vi" /><link rel="alternate" hreflang="en-cn" href="https://bazel.google.cn/versions/7.0.0/start/java" /><link rel="alternate" hreflang="x-default" href="https://bazel.google.cn/versions/7.0.0/start/java" /><link rel="alternate" hreflang="zh-Hans-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=zh-cn" /><link rel="alternate" hreflang="zh-Hant-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=zh-tw" /><link rel="alternate" hreflang="hi-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=hi" /><link rel="alternate" hreflang="id-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=id" /><link rel="alternate" hreflang="ja-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=ja" /><link rel="alternate" hreflang="ko-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=ko" /><link rel="alternate" hreflang="pt-BR-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=pt-br" /><link rel="alternate" hreflang="es-419-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=es-419" /><link rel="alternate" hreflang="th-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=th" /><link rel="alternate" hreflang="tr-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=tr" /><link rel="alternate" hreflang="vi-cn" href="https://bazel.google.cn/versions/7.0.0/start/java?hl=vi" /><title>Bazel Tutorial: Build a Java Project</title> <meta property="og:title" content="Bazel Tutorial: Build a Java Project"><meta property="og:url" content="https://bazel.build/versions/7.0.0/start/java"><meta property="og:locale" content="en"><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "Article", "headline": "Bazel Tutorial: Build a Java Project" } </script> <link rel="stylesheet" href="/extras.css"></head> <body class="" template="page" theme="bazel-theme" type="article" layout="docs" display-toc pending> <devsite-progress type="indeterminate" id="app-progress"></devsite-progress> <section class="devsite-wrapper"> <devsite-cookie-notification-bar></devsite-cookie-notification-bar><devsite-header role="banner"> <div class="devsite-header--inner nocontent"> <div class="devsite-top-logo-row-wrapper-wrapper"> <div class="devsite-top-logo-row-wrapper"> <div class="devsite-top-logo-row"> <button type="button" id="devsite-hamburger-menu" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Navigation menu button" visually-hidden aria-label="Open menu"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="bazel" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/lockup.svg" class="devsite-site-logo" alt="Bazel"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> </li> </ul> </span> </div> <div class="devsite-top-logo-row-middle"> <div class="devsite-header-upper-tabs"> <devsite-tabs class="upper-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Upper tabs"> <tab > <a href="https://bazel.build/versions/7.0.0/about" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/about" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - about bazel" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: About Bazel" track-name="about bazel" > About Bazel </a> </tab> <tab class="devsite-active"> <a href="https://bazel.build/versions/7.0.0/start" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/start" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - getting started" track-metadata-module="primary nav" aria-label="Getting started, selected" data-category="Site-Wide Custom Events" data-label="Tab: Getting started" track-name="getting started" > Getting started </a> </tab> <tab > <a href="https://bazel.build/versions/7.0.0/docs" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/docs" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - user guide" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: User guide" track-name="user guide" > User guide </a> </tab> <tab > <a href="https://bazel.build/versions/7.0.0/reference" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/reference" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - reference" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" > Reference </a> </tab> <tab > <a href="https://bazel.build/versions/7.0.0/extending" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/extending" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - extending" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Extending" track-name="extending" > Extending </a> </tab> <tab > <a href="https://bazel.build/versions/7.0.0/community" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/community" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - community" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Community" track-name="community" > Community </a> </tab> <tab class="devsite-dropdown "> <a href="https://bazel.build/versions" track-metadata-eventdetail="https://bazel.build/versions" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - versioned docs" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Versioned docs" track-name="versioned docs" > Versioned docs </a> <a href="#" role="button" aria-haspopup="true" aria-expanded="false" aria-label="Dropdown menu for Versioned docs" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions" track-metadata-position="nav - versioned docs" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Versioned docs" track-name="versioned docs" class="devsite-tabs-dropdown-toggle devsite-icon devsite-icon-arrow-drop-down"></a> <div class="devsite-tabs-dropdown" aria-label="submenu" hidden> <div class="devsite-tabs-dropdown-content"> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/7.4.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/7.4.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 7.4 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/7.3.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/7.3.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 7.3 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/7.2.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/7.2.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 7.2 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/7.1.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/7.1.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 7.1 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/7.0.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/7.0.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 7.0 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions/6.5.0" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions/6.5.0" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 6.5 </div> </a> </li> <li class="devsite-nav-item"> <a href="https://docs.bazel.build/versions/5.4.1/bazel-overview.html" track-type="nav" track-metadata-eventdetail="https://docs.bazel.build/versions/5.4.1/bazel-overview.html" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> 5.4.1 </div> </a> </li> </ul> </div> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://bazel.build/" track-type="nav" track-metadata-eventdetail="https://bazel.build/" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Nightly </div> </a> </li> <li class="devsite-nav-item"> <a href="https://bazel.build/versions" track-type="nav" track-metadata-eventdetail="https://bazel.build/versions" track-metadata-position="nav - versioned docs" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> More… </div> </a> </li> </ul> </div> </div> </div> </tab> </nav> </devsite-tabs> </div> <devsite-search enable-signin enable-search enable-suggestions enable-query-completion project-name="Bazel" tenant-name="Bazel" > <form class="devsite-search-form" action="https://bazel.build/s/results" method="GET"> <div class="devsite-search-container"> <button type="button" search-open class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Open search"></button> <div class="devsite-searchbox"> <input aria-activedescendant="" aria-autocomplete="list" aria-label="Search" aria-expanded="false" aria-haspopup="listbox" autocomplete="off" class="devsite-search-field devsite-search-query" name="q" placeholder="Search" role="combobox" type="text" value="" > <div class="devsite-search-image material-icons" aria-hidden="true"> </div> <div class="devsite-search-shortcut-icon-container" aria-hidden="true"> <kbd class="devsite-search-shortcut-icon">/</kbd> </div> </div> </div> </form> <button type="button" search-close class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Close search"></button> </devsite-search> </div> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> <a class="devsite-header-link devsite-top-button button gc-analytics-event" href="//github.com/bazelbuild/bazel/" data-category="Site-Wide Custom Events" data-label="Site header link" > GitHub </a> <devsite-user enable-profiles id="devsite-user"> <span class="button devsite-top-button" aria-hidden="true" visually-hidden>Sign in</span> </devsite-user> </div> </div> </div> <div class="devsite-collapsible-section "> <div class="devsite-header-background"> <div class="devsite-product-id-row" > <div class="devsite-product-description-row"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> <a href="https://bazel.build/versions/7.0.0/start" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Lower Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="" > Get started with Bazel quickly </a> </li> </ul> </div> </div> <div class="devsite-doc-set-nav-row"> <devsite-tabs class="lower-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Lower tabs"> <tab > <a href="https://bazel.build/versions/7.0.0/install" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/install" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - install" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Install" track-name="install" > Install </a> </tab> <tab class="devsite-active"> <a href="https://bazel.build/versions/7.0.0/start/cpp" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/start/cpp" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - first build tutorials" track-metadata-module="primary nav" aria-label="First build tutorials, selected" data-category="Site-Wide Custom Events" data-label="Tab: First build tutorials" track-name="first build tutorials" > First build tutorials </a> </tab> <tab > <a href="https://bazel.build/versions/7.0.0/concepts/build-ref" track-metadata-eventdetail="https://bazel.build/versions/7.0.0/concepts/build-ref" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - concepts" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Concepts" track-name="concepts" > Concepts </a> </tab> </nav> </devsite-tabs> </div> </div> </div> </div> </devsite-header> <devsite-book-nav scrollbars > <div class="devsite-book-nav-filter" > <span class="filter-list-icon material-icons" aria-hidden="true"></span> <input type="text" placeholder="Filter" aria-label="Type to filter" role="searchbox"> <span class="filter-clear-button hidden" data-title="Clear filter" aria-label="Clear filter" role="button" tabindex="0"></span> </div> <nav class="devsite-book-nav devsite-nav nocontent" aria-label="Side menu"> <div class="devsite-mobile-header"> <button type="button" id="devsite-close-nav" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close navigation" aria-label="Close navigation"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="bazel" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/lockup.svg" class="devsite-site-logo" alt="Bazel"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> </li> </ul> </span> </div> </div> <div class="devsite-book-nav-wrapper"> <div class="devsite-mobile-nav-top"> <ul class="devsite-nav-list"> <li class="devsite-nav-item"> <a href="/versions/7.0.0/about" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: About Bazel" track-name="about bazel" data-category="Site-Wide Custom Events" data-label="Responsive Tab: About Bazel" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > About Bazel </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/start" class="devsite-nav-title gc-analytics-event devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Getting started" track-name="getting started" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Getting started" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Getting started </span> </a> <ul class="devsite-nav-responsive-tabs"> <li class="devsite-nav-item"> <a href="/versions/7.0.0/install" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Install" track-name="install" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Install" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Install </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/start/cpp" class="devsite-nav-title gc-analytics-event devsite-nav-has-children devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: First build tutorials" track-name="first build tutorials" data-category="Site-Wide Custom Events" data-label="Responsive Tab: First build tutorials" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip menu="_book"> First build tutorials </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="_book"> </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/concepts/build-ref" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Concepts" track-name="concepts" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Concepts" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Concepts </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> </ul> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/docs" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: User guide" track-name="user guide" data-category="Site-Wide Custom Events" data-label="Responsive Tab: User guide" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > User guide </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/reference" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Reference" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Reference </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/extending" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Extending" track-name="extending" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Extending" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Extending </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0/community" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Community" track-name="community" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Community" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Community </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Versioned docs" track-name="versioned docs" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Versioned docs" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Versioned docs </span> </a> <ul class="devsite-nav-responsive-tabs devsite-nav-has-menu "> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Versioned docs" track-name="versioned docs" > <span class="devsite-nav-text" tooltip menu="Versioned docs"> More </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="Versioned docs"> </span> </span> </li> </ul> </li> <li class="devsite-nav-item"> <a href="//github.com/bazelbuild/bazel/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: GitHub" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > GitHub </span> </a> </li> </ul> </div> <div class="devsite-mobile-nav-bottom"> <ul class="devsite-nav-list" menu="_book"> <li class="devsite-nav-item"><a href="/versions/7.0.0/start/cpp" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /versions/7.0.0/start/cpp" track-type="bookNav" track-name="click" track-metadata-eventdetail="/versions/7.0.0/start/cpp" ><span class="devsite-nav-text" tooltip>C++</span></a></li> <li class="devsite-nav-item"><a href="/versions/7.0.0/start/java" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /versions/7.0.0/start/java" track-type="bookNav" track-name="click" track-metadata-eventdetail="/versions/7.0.0/start/java" ><span class="devsite-nav-text" tooltip>Java</span></a></li> <li class="devsite-nav-item"><a href="/versions/7.0.0/start/android-app" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /versions/7.0.0/start/android-app" track-type="bookNav" track-name="click" track-metadata-eventdetail="/versions/7.0.0/start/android-app" ><span class="devsite-nav-text" tooltip>Android App</span></a></li> <li class="devsite-nav-item"><a href="/versions/7.0.0/start/ios-app" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /versions/7.0.0/start/ios-app" track-type="bookNav" track-name="click" track-metadata-eventdetail="/versions/7.0.0/start/ios-app" ><span class="devsite-nav-text" tooltip>iOS App</span></a></li> </ul> <ul class="devsite-nav-list" menu="Versioned docs" aria-label="Side menu" hidden> <li class="devsite-nav-item"> <a href="/versions/7.4.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 7.4" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 7.4 </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.3.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 7.3" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 7.3 </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.2.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 7.2" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 7.2 </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.1.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 7.1" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 7.1 </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/7.0.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 7.0" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 7.0 </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions/6.5.0" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 6.5" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 6.5 </span> </a> </li> <li class="devsite-nav-item"> <a href="https://docs.bazel.build/versions/5.4.1/bazel-overview.html" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: 5.4.1" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > 5.4.1 </span> </a> </li> <li class="devsite-nav-item"> <a href="/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Nightly" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Nightly </span> </a> </li> <li class="devsite-nav-item"> <a href="/versions" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: More…" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > More… </span> </a> </li> </ul> </div> </div> </nav> </devsite-book-nav> <section id="gc-wrapper"> <main role="main" class="devsite-main-content" has-book-nav has-sidebar > <div class="devsite-sidebar"> <div class="devsite-sidebar-content"> <devsite-toc class="devsite-nav" role="navigation" aria-label="On this page" depth="2" scrollbars ></devsite-toc> <devsite-recommendations-sidebar class="nocontent devsite-nav"> </devsite-recommendations-sidebar> </div> </div> <devsite-content> <article class="devsite-article"> <div class="devsite-article-meta nocontent" role="navigation"> <ul class="devsite-breadcrumb-list" aria-label="Breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://bazel.build/" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="Bazel" > Bazel </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://bazel.build/versions/7.0.0/start" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="" > Getting started </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://bazel.build/versions/7.0.0/start/cpp" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="3" track-type="globalNav" track-name="breadcrumb" track-metadata-position="3" track-metadata-eventdetail="" > First build tutorials </a> </li> </ul> <devsite-thumb-rating position="header"> </devsite-thumb-rating> </div> <devsite-feedback position="header" project-name="Bazel" product-id="5052038" bucket="https-bazel-build" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="header" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/touchicon-180.png" > <button> Send feedback </button> </devsite-feedback> <h1 class="devsite-page-title" tabindex="-1"> Bazel Tutorial: Build a Java Project </h1> <devsite-feature-tooltip ack-key="AckCollectionsBookmarkTooltipDismiss" analytics-category="Site-Wide Custom Events" analytics-action-show="Callout Profile displayed" analytics-action-close="Callout Profile dismissed" analytics-label="Create Collection Callout" class="devsite-page-bookmark-tooltip nocontent" dismiss-button="true" id="devsite-collections-dropdown" dismiss-button-text="Dismiss" close-button-text="Got it"> <devsite-bookmark></devsite-bookmark> <span slot="popout-heading"> Stay organized with collections </span> <span slot="popout-contents"> Save and categorize content based on your preferences. </span> </devsite-feature-tooltip> <div class="devsite-page-title-meta"><devsite-view-release-notes></devsite-view-release-notes></div> <devsite-toc class="devsite-nav" depth="2" devsite-toc-embedded > </devsite-toc> <div class="devsite-article-body clearfix "> <a class="button button-with-icon" href="https://github.com/bazelbuild/bazel/issues/new?title=%5Bbazel.build%5D+Problem+with+/versions/7.0.0/start/java&template=doc_issue.yml&link=https%3A%2F%2Fbazel.build/versions/7.0.0/start/java" target="_blank"> Report an issue<span class="material-icons icon-after" aria-hidden="true" translate="no">open_in_new</span> </a> <a class="button button-with-icon" href="https://github.com/bazelbuild/bazel/tree/master/site/en/versions/7.0.0/start/java.md" target="_blank"> View source<span class="material-icons icon-after" aria-hidden="true" translate="no">open_in_new</span> </a> <span style="float: right; line-height: 36px"> <a href="/start/java">Nightly</a> <!-- The lines below are updated by //scripts/docs:gen_new_toc --> <!-- BEGIN_VERSION_INDICATOR --> · <a href="/versions/7.4.0/start/java">7.4</a> . <a href="/versions/7.3.0/start/java">7.3</a> · <a href="/versions/7.2.0/start/java">7.2</a> · <a href="/versions/7.1.0/start/java">7.1</a> · <strong>7.0</strong> · <a href="/versions/6.5.0/start/java">6.5</a> <!-- END_VERSION_INDICATOR --> </span> <p/> <p>This tutorial covers the basics of building Java applications with Bazel. You will set up your workspace and build a simple Java project that illustrates key Bazel concepts, such as targets and <code translate="no" dir="ltr">BUILD</code> files.</p> <p>Estimated completion time: 30 minutes.</p> <h2 id="what_youll_learn" data-text="What you'll learn" tabindex="-1">What you'll learn</h2> <p>In this tutorial you learn how to:</p> <ul> <li>Build a target</li> <li>Visualize the project&#39;s dependencies</li> <li>Split the project into multiple targets and packages</li> <li>Control target visibility across packages</li> <li>Reference targets through labels</li> <li>Deploy a target</li> </ul> <h2 id="before_you_begin" data-text="Before you begin" tabindex="-1">Before you begin</h2> <h3 id="install_bazel" data-text="Install Bazel" tabindex="-1">Install Bazel</h3> <p>To prepare for the tutorial, first <a href="/versions/7.0.0/install">Install Bazel</a> if you don&#39;t have it installed already.</p> <h3 id="install_the_jdk" data-text="Install the JDK" tabindex="-1">Install the JDK</h3> <ol> <li><p>Install Java JDK (preferred version is 11, however versions between 8 and 15 are supported).</p></li> <li><p>Set the JAVA_HOME environment variable to point to the JDK.</p> <ul> <li><p>On Linux/macOS:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">export</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">JAVA_HOME</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-s">"$(dirname $(dirname $(realpath $(which javac))))"</span> </code></pre></devsite-code></li> <li><p>On Windows:</p> <ol> <li>Open Control Panel.</li> <li>Go to &quot;System&nbsp;and&nbsp;Security&quot; &gt; &quot;System&quot; &gt; &quot;Advanced&nbsp;System&nbsp;Settings&quot; &gt; &quot;Advanced&quot;&nbsp;tab &gt; &quot;Environment&nbsp;Variables...&quot; .</li> <li>Under the &quot;User&nbsp;variables&quot; list (the one on the top), click &quot;New...&quot;.</li> <li>In the &quot;Variable&nbsp;name&quot; field, enter <code translate="no" dir="ltr">JAVA_HOME</code>.</li> <li>Click &quot;Browse&nbsp;Directory...&quot;.</li> <li>Navigate to the JDK directory (for example <code translate="no" dir="ltr">C:\Program Files\Java\jdk1.8.0_152</code>).</li> <li>Click &quot;OK&quot; on all dialog windows.</li> </ol></li> </ul></li> </ol> <h3 id="get_the_sample_project" data-text="Get the sample project" tabindex="-1">Get the sample project</h3> <p>Retrieve the sample project from Bazel&#39;s GitHub repository:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">git<span class="devsite-syntax-w"> </span>clone<span class="devsite-syntax-w"> </span>https://github.com/bazelbuild/examples</code></pre></devsite-code> <p>The sample project for this tutorial is in the <code translate="no" dir="ltr">examples/java-tutorial</code> directory and is structured as follows:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">java</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">tutorial</span> <span class="devsite-syntax-err">├──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BUILD</span> <span class="devsite-syntax-err">├──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">src</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">main</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">java</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">com</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">example</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">├──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">cmdline</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">├──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">BUILD</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Runner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">├──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Greeting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span> <span class="devsite-syntax-err">│</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectRunner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">java</span> <span class="devsite-syntax-err">└──</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">WORKSPACE</span> </code></pre></devsite-code> <h2 id="build_with_bazel" data-text="Build with Bazel" tabindex="-1">Build with Bazel</h2> <h3 id="set_up_the_workspace" data-text="Set up the workspace" tabindex="-1">Set up the workspace</h3> <p>Before you can build a project, you need to set up its workspace. A workspace is a directory that holds your project&#39;s source files and Bazel&#39;s build outputs. It also contains files that Bazel recognizes as special:</p> <ul> <li><p>The <code translate="no" dir="ltr">WORKSPACE</code> file, which identifies the directory and its contents as a Bazel workspace and lives at the root of the project&#39;s directory structure,</p></li> <li><p>One or more <code translate="no" dir="ltr">BUILD</code> files, which tell Bazel how to build different parts of the project. (A directory within the workspace that contains a <code translate="no" dir="ltr">BUILD</code> file is a <em>package</em>. You will learn about packages later in this tutorial.)</p></li> </ul> <p>To designate a directory as a Bazel workspace, create an empty file named <code translate="no" dir="ltr">WORKSPACE</code> in that directory.</p> <p>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.</p> <h3 id="understand_the_build_file" data-text="Understand the BUILD file" tabindex="-1">Understand the BUILD file</h3> <p>A <code translate="no" dir="ltr">BUILD</code> file contains several different types of instructions for Bazel. The most important type is the <em>build rule</em>, which tells Bazel how to build the desired outputs, such as executable binaries or libraries. Each instance of a build rule in the <code translate="no" dir="ltr">BUILD</code> file is called a <em>target</em> and points to a specific set of source files and dependencies. A target can also point to other targets.</p> <p>Take a look at the <code translate="no" dir="ltr">java-tutorial/BUILD</code> file:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">java_binary</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"ProjectRunner"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">srcs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">glob</span><span class="devsite-syntax-p">([</span><span class="devsite-syntax-s2">"src/main/java/com/example/*.java"</span><span class="devsite-syntax-p">]),</span> <span class="devsite-syntax-p">)</span> </code></pre></devsite-code> <p>In our example, the <code translate="no" dir="ltr">ProjectRunner</code> target instantiates Bazel&#39;s built-in <a href="/versions/7.0.0/reference/be/java#java_binary"><code translate="no" dir="ltr">java_binary</code> rule</a>. The rule tells Bazel to build a <code translate="no" dir="ltr">.jar</code> file and a wrapper shell script (both named after the target).</p> <p>The attributes in the target explicitly state its dependencies and options. While the <code translate="no" dir="ltr">name</code> attribute is mandatory, many are optional. For example, in the <code translate="no" dir="ltr">ProjectRunner</code> rule target, <code translate="no" dir="ltr">name</code> is the name of the target, <code translate="no" dir="ltr">srcs</code> specifies the source files that Bazel uses to build the target, and <code translate="no" dir="ltr">main_class</code> specifies the class that contains the main method. (You may have noticed that our example uses <a href="/versions/7.0.0/reference/be/functions#glob">glob</a> to pass a set of source files to Bazel instead of listing them one by one.)</p> <h3 id="build_the_project" data-text="Build the project" tabindex="-1">Build the project</h3> <p>To build your sample project, navigate to the <code translate="no" dir="ltr">java-tutorial</code> directory and run:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel<span class="devsite-syntax-w"> </span>build<span class="devsite-syntax-w"> </span>//:ProjectRunner</code></pre></devsite-code> <p>In the target label, the <code translate="no" dir="ltr">//</code> part is the location of the <code translate="no" dir="ltr">BUILD</code> file relative to the root of the workspace (in this case, the root itself), and <code translate="no" dir="ltr">ProjectRunner</code> is the target name in the <code translate="no" dir="ltr">BUILD</code> file. (You will learn about target labels in more detail at the end of this tutorial.)</p> <p>Bazel produces output similar to the following:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code translate="no" dir="ltr"><span class="devsite-syntax-w"> </span>INFO:<span class="devsite-syntax-w"> </span>Found<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">1</span><span class="devsite-syntax-w"> </span>target... <span class="devsite-syntax-w"> </span>Target<span class="devsite-syntax-w"> </span>//:ProjectRunner<span class="devsite-syntax-w"> </span>up-to-date: <span class="devsite-syntax-w"> </span>bazel-bin/ProjectRunner.jar <span class="devsite-syntax-w"> </span>bazel-bin/ProjectRunner <span class="devsite-syntax-w"> </span>INFO:<span class="devsite-syntax-w"> </span>Elapsed<span class="devsite-syntax-w"> </span>time:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">1</span>.021s,<span class="devsite-syntax-w"> </span>Critical<span class="devsite-syntax-w"> </span>Path:<span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">0</span>.83s </code></pre></devsite-code> <p>Congratulations, you just built your first Bazel target! Bazel places build outputs in the <code translate="no" dir="ltr">bazel-bin</code> directory at the root of the workspace. Browse through its contents to get an idea for Bazel&#39;s output structure.</p> <p>Now test your freshly built binary:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel-bin/ProjectRunner</code></pre></devsite-code> <h3 id="review_the_dependency_graph" data-text="Review the dependency graph" tabindex="-1">Review the dependency graph</h3> <p>Bazel requires build dependencies to be explicitly declared in BUILD files. Bazel uses those statements to create the project&#39;s dependency graph, which enables accurate incremental builds.</p> <p>To visualize the sample project&#39;s dependencies, you can generate a text representation of the dependency graph by running this command at the workspace root:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel<span class="devsite-syntax-w"> </span>query<span class="devsite-syntax-w"> </span>--notool_deps<span class="devsite-syntax-w"> </span>--noimplicit_deps<span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"deps(//:ProjectRunner)"</span><span class="devsite-syntax-w"> </span>--output<span class="devsite-syntax-w"> </span>graph</code></pre></devsite-code> <p>The above command tells Bazel to look for all dependencies for the target <code translate="no" dir="ltr">//:ProjectRunner</code> (excluding host and implicit dependencies) and format the output as a graph.</p> <p>Then, paste the text into <a href="http://www.webgraphviz.com/">GraphViz</a>.</p> <p>As you can see, the project has a single target that build two source files with no additional dependencies:</p> <p><img src="/static/versions/7.0.0/docs/images/tutorial_java_01.svg" alt="Dependency graph of the target 'ProjectRunner'"></p> <p>After you set up your workspace, build your project, and examine its dependencies, then you can add some complexity.</p> <h2 id="refine_your_bazel_build" data-text="Refine your Bazel build" tabindex="-1">Refine your Bazel build</h2> <p>While a single target is sufficient for small projects, you may want to split larger projects into multiple targets and packages to allow for fast incremental builds (that is, only rebuild what&#39;s changed) and to speed up your builds by building multiple parts of a project at once.</p> <h3 id="specify_multiple_build_targets" data-text="Specify multiple build targets" tabindex="-1">Specify multiple build targets</h3> <p>You can split the sample project build into two targets. Replace the contents of the <code translate="no" dir="ltr">java-tutorial/BUILD</code> file with the following:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">java_binary</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"ProjectRunner"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">srcs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"src/main/java/com/example/ProjectRunner.java"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">main_class</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"com.example.ProjectRunner"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">deps</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">":greeter"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">java_library</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"greeter"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">srcs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"src/main/java/com/example/Greeting.java"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">)</span> </code></pre></devsite-code> <p>With this configuration, Bazel first builds the <code translate="no" dir="ltr">greeter</code> library, then the <code translate="no" dir="ltr">ProjectRunner</code> binary. The <code translate="no" dir="ltr">deps</code> attribute in <code translate="no" dir="ltr">java_binary</code> tells Bazel that the <code translate="no" dir="ltr">greeter</code> library is required to build the <code translate="no" dir="ltr">ProjectRunner</code> binary.</p> <p>To build this new version of the project, run the following command:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel<span class="devsite-syntax-w"> </span>build<span class="devsite-syntax-w"> </span>//:ProjectRunner</code></pre></devsite-code> <p>Bazel produces output similar to the following:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-nl">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Found</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">target</span><span class="devsite-syntax-p">...</span> <span class="devsite-syntax-n">Target</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">//:ProjectRunner up-to-date:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bazel</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">bin</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">ProjectRunner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jar</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bazel</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">bin</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">ProjectRunner</span> <span class="devsite-syntax-nl">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Elapsed</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">time</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">2.454</span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Critical</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Path</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1.58</span><span class="devsite-syntax-n">s</span> </code></pre></devsite-code> <p>Now test your freshly built binary:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel-bin/ProjectRunner</code></pre></devsite-code> <p>If you now modify <code translate="no" dir="ltr">ProjectRunner.java</code> and rebuild the project, Bazel only recompiles that file.</p> <p>Looking at the dependency graph, you can see that <code translate="no" dir="ltr">ProjectRunner</code> depends on the same inputs as it did before, but the structure of the build is different:</p> <p><img src="/static/docs/images/tutorial_java_02.svg" alt="Dependency graph of the target 'ProjectRunner' after adding a dependency"></p> <p>You&#39;ve now built the project with two targets. The <code translate="no" dir="ltr">ProjectRunner</code> target builds two source files and depends on one other target (<code translate="no" dir="ltr">:greeter</code>), which builds one additional source file.</p> <h3 id="use_multiple_packages" data-text="Use multiple packages" tabindex="-1">Use multiple packages</h3> <p>Let’s now split the project into multiple packages. If you take a look at the <code translate="no" dir="ltr">src/main/java/com/example/cmdline</code> directory, you can see that it also contains a <code translate="no" dir="ltr">BUILD</code> file, plus some source files. Therefore, to Bazel, the workspace now contains two packages, <code translate="no" dir="ltr">//src/main/java/com/example/cmdline</code> and <code translate="no" dir="ltr">//</code> (since there is a <code translate="no" dir="ltr">BUILD</code> file at the root of the workspace).</p> <p>Take a look at the <code translate="no" dir="ltr">src/main/java/com/example/cmdline/BUILD</code> file:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">java_binary</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"runner"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">srcs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"Runner.java"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">main_class</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"com.example.cmdline.Runner"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">deps</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"//:greeter"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">)</span> </code></pre></devsite-code> <p>The <code translate="no" dir="ltr">runner</code> target depends on the <code translate="no" dir="ltr">greeter</code> target in the <code translate="no" dir="ltr">//</code> package (hence the target label <code translate="no" dir="ltr">//:greeter</code>) - Bazel knows this through the <code translate="no" dir="ltr">deps</code> attribute. Take a look at the dependency graph:</p> <p><img src="/static/versions/7.0.0/docs/images/tutorial_java_03.svg" alt="Dependency graph of the target 'runner'"></p> <p>However, for the build to succeed, you must explicitly give the <code translate="no" dir="ltr">runner</code> target in <code translate="no" dir="ltr">//src/main/java/com/example/cmdline/BUILD</code> visibility to targets in <code translate="no" dir="ltr">//BUILD</code> using the <code translate="no" dir="ltr">visibility</code> attribute. This is because by default targets are only visible to other targets in the same <code translate="no" dir="ltr">BUILD</code> file. (Bazel uses target visibility to prevent issues such as libraries containing implementation details leaking into public APIs.)</p> <p>To do this, add the <code translate="no" dir="ltr">visibility</code> attribute to the <code translate="no" dir="ltr">greeter</code> target in <code translate="no" dir="ltr">java-tutorial/BUILD</code> as shown below:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-n">java_library</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">name</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-s2">"greeter"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">srcs</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"src/main/java/com/example/Greeting.java"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-n">visibility</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[</span><span class="devsite-syntax-s2">"//src/main/java/com/example/cmdline:__pkg__"</span><span class="devsite-syntax-p">],</span> <span class="devsite-syntax-p">)</span> </code></pre></devsite-code> <p>Now you can build the new package by running the following command at the root of the workspace:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel<span class="devsite-syntax-w"> </span>build<span class="devsite-syntax-w"> </span>//src/main/java/com/example/cmdline:runner</code></pre></devsite-code> <p>Bazel produces output similar to the following:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-nl">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Found</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">target</span><span class="devsite-syntax-p">...</span> <span class="devsite-syntax-n">Target</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">//src/main/java/com/example/cmdline:runner up-to-date:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bazel</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">bin</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">src</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">main</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">java</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">runner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jar</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bazel</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">bin</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">src</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">main</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">java</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">runner</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Elapsed</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">time</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1.576</span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Critical</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Path</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0.81</span><span class="devsite-syntax-n">s</span> </code></pre></devsite-code> <p>Now test your freshly built binary:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">./bazel-bin/src/main/java/com/example/cmdline/runner</code></pre></devsite-code> <p>You&#39;ve now modified the project to build as two packages, each containing one target, and understand the dependencies between them.</p> <h2 id="use_labels_to_reference_targets" data-text="Use labels to reference targets" tabindex="-1">Use labels to reference targets</h2> <p>In <code translate="no" dir="ltr">BUILD</code> files and at the command line, Bazel uses target labels to reference targets - for example, <code translate="no" dir="ltr">//:ProjectRunner</code> or <code translate="no" dir="ltr">//src/main/java/com/example/cmdline:runner</code>. Their syntax is as follows:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-c1">//path/to/package:target-name</span> </code></pre></devsite-code> <p>If the target is a rule target, then <code translate="no" dir="ltr">path/to/package</code> is the path to the directory containing the <code translate="no" dir="ltr">BUILD</code> file, and <code translate="no" dir="ltr">target-name</code> is what you named the target in the <code translate="no" dir="ltr">BUILD</code> file (the <code translate="no" dir="ltr">name</code> attribute). If the target is a file target, then <code translate="no" dir="ltr">path/to/package</code> is the path to the root of the package, and <code translate="no" dir="ltr">target-name</code> is the name of the target file, including its full path.</p> <p>When referencing targets at the repository root, the package path is empty, just use <code translate="no" dir="ltr">//:target-name</code>. When referencing targets within the same <code translate="no" dir="ltr">BUILD</code> file, you can even skip the <code translate="no" dir="ltr">//</code> workspace root identifier and just use <code translate="no" dir="ltr">:target-name</code>.</p> <p>For example, for targets in the <code translate="no" dir="ltr">java-tutorial/BUILD</code> file, you did not have to specify a package path, since the workspace root is itself a package (<code translate="no" dir="ltr">//</code>), and your two target labels were simply <code translate="no" dir="ltr">//:ProjectRunner</code> and <code translate="no" dir="ltr">//:greeter</code>.</p> <p>However, for targets in the <code translate="no" dir="ltr">//src/main/java/com/example/cmdline/BUILD</code> file you had to specify the full package path of <code translate="no" dir="ltr">//src/main/java/com/example/cmdline</code> and your target label was <code translate="no" dir="ltr">//src/main/java/com/example/cmdline:runner</code>.</p> <h2 id="package_a_java_target_for_deployment" data-text="Package a Java target for deployment" tabindex="-1">Package a Java target for deployment</h2> <p>Let’s now package a Java target for deployment by building the binary with all of its runtime dependencies. This lets you run the binary outside of your development environment.</p> <p>As you remember, the <a href="/versions/7.0.0/reference/be/java#java_binary">java_binary</a> build rule produces a <code translate="no" dir="ltr">.jar</code> and a wrapper shell script. Take a look at the contents of <code translate="no" dir="ltr">runner.jar</code> using this command:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">jar<span class="devsite-syntax-w"> </span>tf<span class="devsite-syntax-w"> </span>bazel-bin/src/main/java/com/example/cmdline/runner.jar</code></pre></devsite-code> <p>The contents are:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">META</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">INF</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">META</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">INF</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">MANIFEST</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MF</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">Runner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">class</span> </code></pre></devsite-code> <p>As you can see, <code translate="no" dir="ltr">runner.jar</code> contains <code translate="no" dir="ltr">Runner.class</code>, but not its dependency, <code translate="no" dir="ltr">Greeting.class</code>. The <code translate="no" dir="ltr">runner</code> script that Bazel generates adds <code translate="no" dir="ltr">greeter.jar</code> to the classpath, so if you leave it like this, it will run locally, but it won&#39;t run standalone on another machine. Fortunately, the <code translate="no" dir="ltr">java_binary</code> rule allows you to build a self-contained, deployable binary. To build it, append <code translate="no" dir="ltr">_deploy.jar</code> to the target name:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">bazel<span class="devsite-syntax-w"> </span>build<span class="devsite-syntax-w"> </span>//src/main/java/com/example/cmdline:runner_deploy.jar</code></pre></devsite-code> <p>Bazel produces output similar to the following:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-nl">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Found</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">target</span><span class="devsite-syntax-p">...</span> <span class="devsite-syntax-n">Target</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">//src/main/java/com/example/cmdline:runner_deploy.jar up-to-date:</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">bazel</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">bin</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">src</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">main</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">java</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">runner_deploy</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">jar</span> <span class="devsite-syntax-nl">INFO</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Elapsed</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">time</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1.700</span><span class="devsite-syntax-n">s</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Critical</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Path</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0.23</span><span class="devsite-syntax-n">s</span> </code></pre></devsite-code> <p>You have just built <code translate="no" dir="ltr">runner_deploy.jar</code>, which you can run standalone away from your development environment since it contains the required runtime dependencies. Take a look at the contents of this standalone JAR using the same command as before:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Bash"><code class="devsite-terminal" translate="no" dir="ltr">jar<span class="devsite-syntax-w"> </span>tf<span class="devsite-syntax-w"> </span>bazel-bin/src/main/java/com/example/cmdline/runner_deploy.jar</code></pre></devsite-code> <p>The contents include all of the necessary classes to run:</p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"><span class="devsite-syntax-n">META</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">INF</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">META</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">INF</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">MANIFEST</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">MF</span> <span class="devsite-syntax-n">build</span><span class="devsite-syntax-o">-</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">properties</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">cmdline</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">Runner</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">class</span> <span class="devsite-syntax-n">com</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">example</span><span class="devsite-syntax-o">/</span><span class="devsite-syntax-n">Greeting</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">class</span> </code></pre></devsite-code> <h2 id="further_reading" data-text="Further reading" tabindex="-1">Further reading</h2> <p>For more details, see:</p> <ul> <li><p><a href="https://github.com/bazelbuild/rules_jvm_external">rules_jvm_external</a> for rules to manage transitive Maven dependencies.</p></li> <li><p><a href="/versions/7.0.0/docs/external">External Dependencies</a> to learn more about working with local and remote repositories.</p></li> <li><p>The <a href="/versions/7.0.0/rules">other rules</a> to learn more about Bazel.</p></li> <li><p>The <a href="/versions/7.0.0/start/cpp">C++ build tutorial</a> to get started with building C++ projects with Bazel.</p></li> <li><p>The <a href="/versions/7.0.0/start/android-app">Android application tutorial</a> and <a href="/versions/7.0.0/start/ios-app">iOS application tutorial</a>) to get started with building mobile applications for Android and iOS with Bazel.</p></li> </ul> <p>Happy building!</p> </div> <devsite-thumb-rating position="footer"> </devsite-thumb-rating> <devsite-feedback position="footer" project-name="Bazel" product-id="5052038" bucket="https-bazel-build" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="footer" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/touchicon-180.png" > <button> Send feedback </button> </devsite-feedback> <div class="devsite-floating-action-buttons"> </div> </article> <devsite-content-footer class="nocontent"> <p>Except as otherwise noted, the content of this page is licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For details, see the <a href="https://developers.google.com/site-policies">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p> <p>Last updated 2024-10-22 UTC.</p> </devsite-content-footer> <devsite-notification > </devsite-notification> <div class="devsite-content-data"> <template class="devsite-thumb-rating-feedback"> <devsite-feedback position="thumb-rating" project-name="Bazel" product-id="5052038" bucket="https-bazel-build" context="" version="t-devsite-webserver-20241114-r00-rc02.464921008191574316" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="thumb-rating" class="nocontent" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/touchicon-180.png" > <button> Need to tell us more? </button> </devsite-feedback> </template> <template class="devsite-content-data-template"> [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-22 UTC."],[],[]] </template> </div> </devsite-content> </main> <devsite-footer-promos class="devsite-footer"> </devsite-footer-promos> <devsite-footer-linkboxes class="devsite-footer"> <nav class="devsite-footer-linkboxes nocontent" aria-label="Footer links"> <ul class="devsite-footer-linkboxes-list"> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">About</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/community/users" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Who's using Bazel </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/contribute/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > Contribute </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/contribute/contribution-policy" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Governance model </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/release" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" > Release model </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/brand" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" > Brand guidelines </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Stay connected</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="//blog.bazel.build" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Blog </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//github.com/bazelbuild/bazel" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > GitHub </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//twitter.com/bazelbuild" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Twitter </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//youtube.com/user/googleOSPO" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" > YouTube </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Support</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/help" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" > Support </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//github.com/bazelbuild/bazel/issues" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" > Issue tracker </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//slack.bazel.build" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" > Slack </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//stackoverflow.com/questions/tagged/bazel" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" > Stack Overflow </a> </li> </ul> </li> </ul> </nav> </devsite-footer-linkboxes> <devsite-footer-utility class="devsite-footer"> <div class="devsite-footer-utility nocontent"> <nav class="devsite-footer-utility-links" aria-label="Utility links"> <ul class="devsite-footer-utility-list"> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/terms" data-category="Site-Wide Custom Events" data-label="Footer Terms link" > Terms </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/privacy" data-category="Site-Wide Custom Events" data-label="Footer Privacy link" > Privacy </a> </li> <li class="devsite-footer-utility-item glue-cookie-notification-bar-control"> <a class="devsite-footer-utility-link gc-analytics-event" href="#" data-category="Site-Wide Custom Events" data-label="Footer Manage cookies link" aria-hidden="true" > Manage cookies </a> </li> </ul> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="vi" >Tiếng Việt</a> </li> <li role="presentation"> <a role="menuitem" lang="tr" >Türkçe</a> </li> <li role="presentation"> <a role="menuitem" lang="hi" >हिंदी</a> </li> <li role="presentation"> <a role="menuitem" lang="th" >ภาษาไทย</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_tw" >中文 – 繁體</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> </nav> </div> </devsite-footer-utility> <devsite-panel></devsite-panel> </section></section> <devsite-sitemask></devsite-sitemask> <devsite-snackbar></devsite-snackbar> <devsite-tooltip ></devsite-tooltip> <devsite-heading-link></devsite-heading-link> <devsite-analytics> <script type="application/json" analytics>[{&#34;dimensions&#34;: {&#34;dimension3&#34;: &#34;en&#34;, &#34;dimension2&#34;: false, &#34;dimension1&#34;: &#34;Signed out&#34;, &#34;dimension4&#34;: &#34;en&#34;, &#34;dimension5&#34;: false}, &#34;gaid&#34;: &#34;UA-61082125-3&#34;, &#34;metrics&#34;: {}, &#34;purpose&#34;: 0}]</script> <script type="application/json" tag-management>{&#34;at&#34;: &#34;True&#34;, &#34;ga4&#34;: [{&#34;id&#34;: &#34;G-GBZW986TQ3&#34;, &#34;purpose&#34;: 0}], &#34;ga4p&#34;: [{&#34;id&#34;: &#34;G-GBZW986TQ3&#34;, &#34;purpose&#34;: 0}], &#34;gtm&#34;: [], &#34;parameters&#34;: {&#34;internalUser&#34;: &#34;False&#34;, &#34;language&#34;: {&#34;machineTranslated&#34;: &#34;False&#34;, &#34;requested&#34;: &#34;en&#34;, &#34;served&#34;: &#34;en&#34;}, &#34;pageType&#34;: &#34;article&#34;, &#34;projectName&#34;: &#34;Bazel&#34;, &#34;signedIn&#34;: &#34;False&#34;, &#34;tenant&#34;: &#34;bazel&#34;, &#34;recommendations&#34;: {&#34;sourcePage&#34;: &#34;&#34;, &#34;sourceType&#34;: 0, &#34;sourceRank&#34;: 0, &#34;sourceIdenticalDescriptions&#34;: 0, &#34;sourceTitleWords&#34;: 0, &#34;sourceDescriptionWords&#34;: 0, &#34;experiment&#34;: &#34;&#34;}, &#34;experiment&#34;: {&#34;ids&#34;: &#34;&#34;}}}</script> </devsite-analytics> <devsite-badger></devsite-badger> <script nonce="kYcYnejpRpAhBeSL/9i3tNuYaYHE/b"> (function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i; t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0]; E.parentNode.insertBefore(t,E);})(window, document, 'script', 'https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/js/app_loader.js', '[40,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel","https://bazel-dot-devsite-v2-prod-3p.appspot.com",null,null,["/_pwa/bazel/manifest.json","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/images/video-placeholder.svg","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/favicon-prod.png","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/bazel/images/lockup.svg","https://fonts.googleapis.com/css?family=Roboto:300,400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"],1,null,[1,6,8,12,14,17,21,25,50,52,63,70,75,76,80,87,91,92,93,97,98,100,101,102,103,104,105,107,108,109,110,112,113,116,117,118,120,122,124,125,126,127,129,130,131,132,133,134,135,136,138,140,141,147,148,149,151,152,156,157,158,159,161,163,164,168,169,170,179,180,182,183,186,191,193,196],"AIzaSyCNm9YxQumEXwGJgTDjxoxXK6m1F-9720Q","AIzaSyCc76DZePGtoyUjqKrLdsMGk_ry7sljLbY","bazel.build","AIzaSyB9bqgQ2t11WJsOX8qNsCQ6U-w91mmqF-I","AIzaSyAdYnStPdzjcJJtQ0mvIaeaMKj7_t6J_Fg",null,null,null,["EngEduTelemetry__enable_engedu_telemetry","CloudShell__cloud_shell_button","Profiles__enable_release_notes_notifications","SignIn__enable_oauth_multi_account_support","Cloud__enable_cloud_dlp_service","CloudShell__cloud_code_overflow_menu","TpcFeatures__enable_required_headers","Cloud__enable_legacy_calculator_redirect","Experiments__reqs_query_experiments","Concierge__enable_pushui","Profiles__enable_profile_collections","Profiles__enable_complete_playlist_endpoint","MiscFeatureFlags__developers_footer_dark_image","MiscFeatureFlags__enable_explain_this_code","MiscFeatureFlags__enable_view_transitions","Search__enable_page_map","Profiles__require_profile_eligibility_for_signin","SignIn__enable_auto_login_multi_account","Profiles__enable_page_saving","Profiles__enable_developer_profiles_callout","MiscFeatureFlags__enable_variable_operator","Profiles__enable_awarding_url","Cloud__enable_cloudx_ping","Profiles__enable_completecodelab_endpoint","OnSwitch__enable","SignIn__enable_refresh_access_tokens","Analytics__enable_clearcut_logging","Search__enable_suggestions_from_borg","Cloud__enable_cloud_shell_fte_user_flow","TpcFeatures__enable_mirror_tenant_redirects","Cloud__enable_llm_concierge_chat","Profiles__enable_dashboard_curated_recommendations","MiscFeatureFlags__enable_project_variables","MiscFeatureFlags__developers_footer_image","Profiles__enable_public_developer_profiles","DevPro__enable_cloud_innovators_plus","DevPro__enable_developer_subscriptions","BookNav__enable_tenant_cache_key","Cloud__enable_free_trial_server_call","Profiles__enable_recognition_badges","Search__enable_ai_eligibility_checks","MiscFeatureFlags__emergency_css","Cloud__enable_cloud_shell","Cloud__enable_cloud_facet_chat","Cloud__enable_cloudx_experiment_ids","MiscFeatureFlags__enable_firebase_utm","Search__enable_dynamic_content_confidential_banner"],null,null,"AIzaSyA58TaKli1DculwmAmbpzLVGuWc8eCQgQc","https://developerscontentserving-pa.googleapis.com","AIzaSyDWBU60w0P9hEkr29kkksYs8Z7gvZ8u_wc","https://developerscontentsearch-pa.googleapis.com",2,4,null,"https://developerprofiles-pa.googleapis.com",[40,"bazel","Bazel","bazel.build",null,"bazel-dot-devsite-v2-prod-3p.appspot.com",null,null,[null,1,null,null,null,null,null,null,null,null,null,[1],null,null,null,null,null,null,[1],null,null,null,null,[1,1,1],[1,1,null,1,1]],null,[56,null,null,null,null,null,"/images/lockup.svg",null,null,null,null,1,null,null,null,null,null,null,null,null,null,1,null,null,null,null,[]],[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[6,7,1,18,20,22,23,29,37,39,40,43],null,[[],[1,1]],[[["UA-61082125-3"],["UA-61082125-4"],null,null,["UA-61082125-5"],null,null,[["G-GBZW986TQ3"],null,null,[["G-GBZW986TQ3",1]]],[["UA-61082125-3",1]],null,[["UA-61082125-5",1]],null,1],[[4,5],[1,1],[5,8],[3,4],[2,2]]],null,4]]') </script> <devsite-a11y-announce></devsite-a11y-announce> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10