Prepare unit tests for dropping java rules from `@_builtins`

Mechanical change that just adds `load()`s everywhere

PiperOrigin-RevId: 682671088
Change-Id: I190d559accae12d71f5c510abc02d5d306a41b7b
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AbstractConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AbstractConfiguredTargetTest.java
index 2abc0f2..627e8cf 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AbstractConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AbstractConfiguredTargetTest.java
@@ -44,6 +44,8 @@
         scratchConfiguredTarget(
             "java/a",
             "a",
+            "load('@rules_java//java:defs.bzl', 'java_binary',"
+                + " 'java_library')",
             "java_binary(name='a', srcs=['A.java'], deps=[':b'])",
             "java_library(name='b', srcs=['B.java'])");
 
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
index 5a15674..4d514d3 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AnalysisCachingTest.java
@@ -78,6 +78,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -94,6 +95,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -114,6 +116,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -131,6 +134,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -153,6 +157,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -162,6 +167,7 @@
     scratch.file(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "b",
             srcs = ["B.java"],
@@ -172,6 +178,7 @@
     scratch.overwriteFile(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "b",
             srcs = ["C.java"],
@@ -247,6 +254,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -256,6 +264,7 @@
     scratch.file(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "b",
             srcs = ["B.java"],
@@ -266,6 +275,7 @@
     scratch.overwriteFile(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -640,6 +650,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -649,6 +660,7 @@
     scratch.file(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "b",
             srcs = ["B.java"],
@@ -658,6 +670,7 @@
     scratch.file(
         "java/c/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "c",
             srcs = ["C.java"],
@@ -672,6 +685,7 @@
     scratch.overwriteFile(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "b",
             srcs = ["B.java"],
@@ -694,6 +708,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -709,6 +724,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "x",
             srcs = ["A.java"],
@@ -735,6 +751,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         # It's important that all targets are of the same rule class, otherwise the second update
         # call might analyze more than one extra target because of potential implicit dependencies.
         java_library(
@@ -769,8 +786,18 @@
 
   @Test
   public void testExtraActions() throws Exception {
-    scratch.file("java/com/google/a/BUILD", "java_library(name='a', srcs=['A.java'])");
-    scratch.file("java/com/google/b/BUILD", "java_library(name='b', srcs=['B.java'])");
+    scratch.file(
+        "java/com/google/a/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_library")
+        java_library(name='a', srcs=['A.java'])
+        """);
+    scratch.file(
+        "java/com/google/b/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_library")
+        java_library(name='b', srcs=['B.java'])
+        """);
     scratch.file(
         "extra/BUILD",
         """
@@ -794,7 +821,12 @@
 
   @Test
   public void testExtraActionsCaching() throws Exception {
-    scratch.file("java/a/BUILD", "java_library(name='a', srcs=['A.java'])");
+    scratch.file(
+        "java/a/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_library")
+        java_library(name='a', srcs=['A.java'])
+        """);
     scratch.file(
         "extra/BUILD",
         """
@@ -853,6 +885,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -865,6 +898,7 @@
     scratch.overwriteFile(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["B.java"],
@@ -885,6 +919,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "A",
             srcs = ["A.java"],
@@ -894,6 +929,7 @@
     scratch.file(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "B",
             srcs = ["B.java"],
@@ -910,6 +946,7 @@
     scratch.overwriteFile(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "B",
             srcs = ["C.java"],
@@ -942,6 +979,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "x",
             srcs = ["A.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
index bc7837e..540c6ee 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AspectTest.java
@@ -939,8 +939,8 @@
     setRulesAndAspectsAvailableInTests(ImmutableList.of(aspectApplyingToFiles), ImmutableList.of());
     pkg(
         "a",
-        "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])"
-    );
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
+        "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])");
 
     var collector = new AspectConfiguredCollector();
     eventBus.register(collector);
@@ -975,8 +975,8 @@
     setRulesAndAspectsAvailableInTests(ImmutableList.of(aspectApplyingToFiles), ImmutableList.of());
     pkg(
         "a",
-        "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])"
-    );
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
+        "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])");
     scratch.file("a/x.java", "");
     AnalysisResult analysisResult = update(new EventBus(), defaultFlags(),
         ImmutableList.of(aspectApplyingToFiles.getName()),
@@ -992,6 +992,7 @@
     pkg("b");
     pkg(
         "a",
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
         "package_group(name = 'group', packages = ['//b'])",
         "java_binary(name = 'x', main_class = 'x.F', srcs = ['x.java'], visibility = [':group'])");
     scratch.file("a/x.java", "");
@@ -1010,7 +1011,10 @@
   public void duplicateTopLevelAspects_duplicateAspectsNotAllowed() throws Exception {
     AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles();
     setRulesAndAspectsAvailableInTests(ImmutableList.of(aspectApplyingToFiles), ImmutableList.of());
-    pkg("a", "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])");
+    pkg(
+        "a",
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
+        "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])");
     reporter.removeHandler(failFastHandler);
 
     assertThrows(
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/AutoExecGroupsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/AutoExecGroupsTest.java
index ca13906..d0e05c2 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/AutoExecGroupsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/AutoExecGroupsTest.java
@@ -83,6 +83,7 @@
     scratch.overwriteFile(
         "rule/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_runtime")
         exports_files(["test_toolchain/bzl"])
 
         toolchain_type(name = "toolchain_type_1")
@@ -1196,6 +1197,7 @@
     scratch.file(
         "java/com/google/optimizationtest/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "optimizer",
             srcs = ["Foo.java"],
@@ -1205,6 +1207,7 @@
         """);
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1249,6 +1252,7 @@
     scratch.file(
         "java/com/google/optimizationtest/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "optimizer",
             srcs = ["Foo.java"],
@@ -1258,6 +1262,7 @@
         """);
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1298,6 +1303,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1333,6 +1339,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1368,6 +1375,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1391,6 +1399,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         load("//test:defs.bzl", "custom_rule")
 
         java_plugin(
@@ -1423,6 +1432,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1446,6 +1456,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         load("//test:defs.bzl", "custom_rule")
 
         java_plugin(
@@ -1478,6 +1489,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1521,6 +1533,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1565,6 +1578,7 @@
           throws Exception {
     scratch.file(
         "bazel_internal/test_rules/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1613,6 +1627,7 @@
           throws Exception {
     scratch.file(
         "bazel_internal/test_rules/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -1660,6 +1675,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  ctx.actions.run(",
@@ -1702,6 +1718,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  source_jar = java_common.pack_sources(",
@@ -1739,6 +1756,7 @@
       throws Exception {
     scratch.file(
         "test/defs.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
         "  ctx.actions.run(",
@@ -2416,6 +2434,8 @@
     scratch.file(TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/jdk/tzdata.jar");
     scratch.overwriteFile(
         TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/jdk/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime',"
+            + " 'java_toolchain')",
         "load(",
         "    ':java_toolchain_alias.bzl',",
         "    'java_toolchain_alias',",
@@ -2464,6 +2484,8 @@
         ")");
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common', 'JavaInfo',"
+            + " 'JavaPluginInfo')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -2492,6 +2514,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_plugin(
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java b/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
index 3a05a43..400afa9 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/BuildViewTest.java
@@ -271,6 +271,7 @@
     scratch.file(
         "java/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name='library_invalid_dep',
             srcs=['NoOp.java'],
@@ -312,6 +313,7 @@
     scratch.file(
         "java/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name='library_invalid_dep',
             srcs=['NoOp.java'],
@@ -349,6 +351,7 @@
     scratch.file(
         "java/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         package_group(name = 'group', includes=['//non/existent/package:othergroup'])
         java_library(
             name='library_invalid_visibility',
@@ -563,6 +566,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(name = 'a',
           srcs = ['A.java'],
           deps = ['//java/b'])
@@ -581,6 +585,7 @@
     scratch.file(
         "java/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(':rules.bzl', 'gen')
         gen(name='src')
         java_library(name = 'b', srcs = [':src'])
@@ -763,6 +768,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(name = 'A',
                   srcs = glob(['A*.java']))
         java_test(name = 'B',
@@ -1100,6 +1106,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(name = 'java', srcs = ['DoesntMatter.java'])
         cc_binary(name = 'cpp', data = [':java'])
         """);
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java b/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
index 1fe61d8..607e853 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/CircularDependencyTest.java
@@ -104,7 +104,10 @@
   public void testOneRuleImplicitCycleJava() throws Exception {
     Package pkg =
         createScratchPackageForImplicitCycle(
-            "cycle", "java_library(name='jcyc',", "      srcs = ['libjcyc.jar', 'foo.java'])");
+            "cycle",
+            "load('@rules_java//java:defs.bzl', 'java_library')",
+            "java_library(name='jcyc',",
+            "      srcs = ['libjcyc.jar', 'foo.java'])");
     assertThrows(NoSuchTargetException.class, () -> pkg.getTarget("jcyc"));
     assertThat(pkg.containsErrors()).isTrue();
     assertContainsEvent("rule 'jcyc' has file 'libjcyc.jar' as both an" + " input and an output");
@@ -148,6 +151,7 @@
     scratch.file(
         "x/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "x",
             deps = ["y"],
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java b/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
index 82ad6e45..7a07dfa 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/ConfigurableAttributesTest.java
@@ -64,7 +64,9 @@
   }
 
   private void writeHelloRules(boolean includeDefaultCondition) throws IOException {
-    scratch.file("java/hello/BUILD",
+    scratch.file(
+        "java/hello/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary', 'java_library')",
         "java_binary(",
         "    name = 'hello',",
         "    srcs = ['hello.java'],",
@@ -232,7 +234,9 @@
   @Test
   public void depsWithDuplicatesInDifferentBranches() throws Exception {
     writeConfigRules();
-    scratch.file("java/hello/BUILD",
+    scratch.file(
+        "java/hello/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary', 'java_library')",
         "java_binary(",
         "    name = 'hello',",
         "    srcs = ['hello.java'],",
@@ -264,7 +268,9 @@
   @Test
   public void depsWithDuplicatesInSameBranch() throws Exception {
     writeConfigRules();
-    scratch.file("java/hello/BUILD",
+    scratch.file(
+        "java/hello/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary', 'java_library')",
         "java_binary(",
         "    name = 'hello',",
         "    srcs = ['hello.java'],",
@@ -302,6 +308,7 @@
     scratch.file(
         "java/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'hello',
             srcs = select({
@@ -332,6 +339,7 @@
     scratch.file(
         "java/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'hello',
             srcs = select({
@@ -358,6 +366,7 @@
     scratch.file(
         "java/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'hello',
             srcs = select({
@@ -450,6 +459,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'int_key',
             srcs = select({123: ['a.java']})
@@ -465,6 +475,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'bool_key',
             srcs = select({True: ['a.java']})
@@ -480,6 +491,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'none_key',
             srcs = select({None: ['a.java']})
@@ -719,6 +731,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         java_binary(
             name = 'binary',
             srcs = ['binary.java'],
@@ -803,6 +816,7 @@
     scratch.file(
         "java/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         java_binary(
             name = 'binary',
             srcs = ['binary.java'],
@@ -860,6 +874,7 @@
     scratch.file(
         "java/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'hello_default_no_match_error',
             srcs = select({
@@ -925,6 +940,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         load(':rule.bzl', 'myrule')
         myrule(
             name = 'mytarget',
@@ -983,6 +999,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         load(':rule.bzl', 'myrule')
         myrule(
             name = 'mytarget',
@@ -1040,6 +1057,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         load(':rule.bzl', 'myrule')
         myrule(
             name = 'mytarget',
@@ -1112,6 +1130,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(':rule.bzl', 'myrule')
         myrule(
             name = 'mytarget',
@@ -1164,6 +1183,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'binary',
             srcs = select({
@@ -1185,6 +1205,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'binary',
             srcs = glob(['globbed/*.java']) + select({
@@ -1230,6 +1251,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = 'binary',
             srcs = ['binary.java'] + select({
@@ -2099,6 +2121,7 @@
     scratch.file(
         "java/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//java:macros.bzl', 'my_java_binary')
         my_java_binary(
             name = 'binary',
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetTest.java
index 3b61a9e..cbfcdc8 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetTest.java
@@ -399,11 +399,17 @@
 
   @Test
   public void testNoWarningWhenDeprecatedDependsOnDeprecatedRule() throws Exception {
-    scratch.file("foo/BUILD",
+    scratch.file(
+        "foo/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='foo', srcs=['foo.java'], deps=['//bar:bar'])");
-    scratch.file("bar/BUILD",
+    scratch.file(
+        "bar/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='bar', srcs=['bar.java'], deps=['//baz:baz'], deprecation='BAR')");
-    scratch.file("baz/BUILD",
+    scratch.file(
+        "baz/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='baz', srcs=['baz.java'], deprecation='BAZ')");
 
     reporter.removeHandler(failFastHandler); // expect errors
@@ -424,18 +430,23 @@
             // error:
             getErrorNonExistingTarget("srcs", "java_library", "//x:x", "//x:a.cc"),
             // build file:
+            "load('@rules_java//java:defs.bzl', 'java_library')",
             "# blank line",
             "java_library(name = 'x',",
             "           srcs = ['a.cc'])");
-    assertThat(e.getLocation().toString()).isEqualTo("/workspace/x/BUILD:2:13");
+    assertThat(e.getLocation().toString()).isEqualTo("/workspace/x/BUILD:3:13");
   }
 
   @Test
   public void testJavatestsIsTestonly() throws Exception {
-    scratch.file("java/x/BUILD",
-                "java_library(name='x', exports=['//javatests/y'])");
-    scratch.file("javatests/y/BUILD",
-                "java_library(name='y')");
+    scratch.file(
+        "java/x/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name='x', exports=['//javatests/y'])");
+    scratch.file(
+        "javatests/y/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name='y')");
     reporter.removeHandler(failFastHandler); // expect warning
     ConfiguredTarget target = getConfiguredTarget("//java/x");
     assertContainsEvent("non-test target '//java/x:x' depends on testonly target"
@@ -448,6 +459,7 @@
     scratch.file(
         "java/banana/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "banana",
             visibility = ["//javatests/plantain:chips"],
@@ -469,23 +481,31 @@
   @Test
   public void testUnexpectedSourceFileInDeps() throws Exception {
     scratch.file("x/y.java", "foo");
-    checkError("x", "x", getErrorMsgMisplacedFiles(
-        "deps", "java_library", "//x:x", "//x:y.java"),
+    checkError(
+        "x",
+        "x",
+        getErrorMsgMisplacedFiles("deps", "java_library", "//x:x", "//x:y.java"),
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='x', srcs=['x.java'], deps=['y.java'])");
   }
 
   @Test
   public void testUnexpectedButExistingSourceFileDependency() throws Exception {
     scratch.file("x/y.java");
-    checkError("x", "x", getErrorMsgMisplacedFiles(
-        "deps", "java_library", "//x:x", "//x:y.java"),
+    checkError(
+        "x",
+        "x",
+        getErrorMsgMisplacedFiles("deps", "java_library", "//x:x", "//x:y.java"),
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='x', srcs=['x.java'], deps=['y.java'])");
   }
 
   @Test
   public void testGetArtifactForImplicitOutput() throws Exception {
-    scratch.file("java/x/BUILD",
-                "java_binary(name='x', srcs=['x.java'])");
+    scratch.file(
+        "java/x/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
+        "java_binary(name='x', srcs=['x.java'])");
 
     ConfiguredTarget javaBinary = getConfiguredTarget("//java/x:x");
     Artifact classJarArtifact = getFileConfiguredTarget("//java/x:x.jar").getArtifact();
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/actions/PathMappersTest.java b/src/test/java/com/google/devtools/build/lib/analysis/actions/PathMappersTest.java
index 5d3aee0..cafd116 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/actions/PathMappersTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/actions/PathMappersTest.java
@@ -50,6 +50,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         genrule(
             name = 'gen_b',
             outs = ['B.java'],
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/constraints/ConstraintsTest.java b/src/test/java/com/google/devtools/build/lib/analysis/constraints/ConstraintsTest.java
index 2bafd4a..a90a0ba 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/constraints/ConstraintsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/constraints/ConstraintsTest.java
@@ -858,6 +858,7 @@
     scratch.file(
         "hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'hi',
             data = ['//helpers:default'],
@@ -1521,6 +1522,7 @@
     scratch.file(
         "hello/a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'a',
             runtime_deps = ['//hello/b'],
@@ -1529,6 +1531,7 @@
     scratch.file(
         "hello/b/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'b',
             runtime_deps = select({'//hello/c': []}),
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/starlark/StarlarkSubruleTest.java b/src/test/java/com/google/devtools/build/lib/analysis/starlark/StarlarkSubruleTest.java
index 66e455d..7268e4c 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/starlark/StarlarkSubruleTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/starlark/StarlarkSubruleTest.java
@@ -363,6 +363,7 @@
     scratch.file(
         "subrule_testing/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load("myrule.bzl", "my_rule")
 
         java_library(name = "bar")
@@ -504,6 +505,7 @@
     scratch.file(
         "subrule_testing/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load("myrule.bzl", "my_rule")
 
         java_library(name = "bar")
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/test/InstrumentedFileManifestActionTest.java b/src/test/java/com/google/devtools/build/lib/analysis/test/InstrumentedFileManifestActionTest.java
index ec69220..f879bb0 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/test/InstrumentedFileManifestActionTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/test/InstrumentedFileManifestActionTest.java
@@ -47,6 +47,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "foo.so",
             srcs = ["Bar.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java b/src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java
index d90221e..e099bda 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/test/TestActionBuilderTest.java
@@ -125,6 +125,7 @@
     scratch.file(
         "javatests/jt/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "RGT",
             srcs = ["RGT.java"],
@@ -173,6 +174,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         sh_test(
             name = "sh",
             srcs = ["a.sh"],
@@ -329,6 +331,7 @@
     scratch.file(
         "javatests/timeouts/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "small_no_timeout",
             size = "small",
@@ -361,6 +364,7 @@
     scratch.file(
         "javatests/jt/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         java_test(
             name = "RGT",
             srcs = ["RGT.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java b/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java
index 89efffa..42f0601 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java
@@ -1050,6 +1050,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_test")
         load(":lib.bzl", "starlark_lib")
 
         starlark_lib(
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestBase.java b/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestBase.java
index 178745d..b4f56bb 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestBase.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/util/BuildViewTestBase.java
@@ -53,10 +53,18 @@
   protected void runAnalysisWithOutputFilter(Pattern outputFilter) throws Exception {
     scratch.file("java/a/BUILD",
         "exports_files(['A.java'])");
-    scratch.file("java/b/BUILD",
-        "java_library(name = 'b', srcs = ['//java/a:A.java'])");
-    scratch.file("java/c/BUILD",
-        "java_library(name = 'c', exports = ['//java/b:b'])");
+    scratch.file(
+        "java/b/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_library")
+        java_library(name = 'b', srcs = ['//java/a:A.java'])
+        """);
+    scratch.file(
+        "java/c/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_library")
+        java_library(name = 'c', exports = ['//java/b:b'])
+        """);
     reporter.setOutputFilter(RegexOutputFilter.forPattern(outputFilter));
     update("//java/c:c");
   }
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/google/CoverageCommandUnitTest.java b/src/test/java/com/google/devtools/build/lib/bazel/google/CoverageCommandUnitTest.java
index 90de7ba..da851eb 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/google/CoverageCommandUnitTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/google/CoverageCommandUnitTest.java
@@ -30,6 +30,7 @@
     scratch.file(
         "javatests/com/google/foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "l",
             srcs = ["foo.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelLicensingTests.java b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelLicensingTests.java
index 03684e9..c126692 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelLicensingTests.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/BazelLicensingTests.java
@@ -30,6 +30,7 @@
     scratch.file(
         "ise/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         licenses(["restricted"])
 
         java_library(
@@ -48,6 +49,7 @@
     scratch.file(
         "gsa/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         licenses(["unencumbered"])
 
         java_library(
diff --git a/src/test/java/com/google/devtools/build/lib/bazel/rules/java/JavaConfiguredTargetsTest.java b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/JavaConfiguredTargetsTest.java
index 58eb692..b6f8347 100644
--- a/src/test/java/com/google/devtools/build/lib/bazel/rules/java/JavaConfiguredTargetsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/bazel/rules/java/JavaConfiguredTargetsTest.java
@@ -77,6 +77,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "bin",
             srcs = ["Foo.java"],
@@ -98,6 +99,7 @@
   public void javaTestSetsSecurityManagerPropertyOnVersion17() throws Exception {
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_test', 'java_runtime')",
         "java_runtime(",
         "    name = 'jvm',",
         "    java = 'java_home/bin/java',",
@@ -124,7 +126,12 @@
   // regression test for https://github.com/bazelbuild/bazel/issues/20378
   @Test
   public void javaTestInvalidTestClassAtRootPackage() throws Exception {
-    scratch.file("BUILD", "java_test(name = 'some_test', srcs = ['SomeTest.java'])");
+    scratch.file(
+        "BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_test")
+        java_test(name = 'some_test', srcs = ['SomeTest.java'])
+        """);
     invalidatePackages();
 
     AssertionError error =
diff --git a/src/test/java/com/google/devtools/build/lib/buildtool/ActionListenerIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/buildtool/ActionListenerIntegrationTest.java
index c8a6826..d64f6aa 100644
--- a/src/test/java/com/google/devtools/build/lib/buildtool/ActionListenerIntegrationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/buildtool/ActionListenerIntegrationTest.java
@@ -188,14 +188,16 @@
 
   @Test
   public void testBasicActionListener() throws Exception {
-    write("nobuild/BUILD",
+    write(
+        "nobuild/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name= 'javalib',",
         "             srcs=[])",
         "extra_action(name = 'baz',",
         "             out_templates = ['$(OWNER_LABEL_DIGEST)_$(ACTION_ID).tst'],",
-        "             cmd = " +
-        "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)" +
-            "_$(ACTION_ID).tst)')",
+        "             cmd = "
+            + "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)"
+            + "_$(ACTION_ID).tst)')",
         "action_listener(name = 'bar',",
         "                mnemonics = ['Javac'],",
         "                extra_actions = [':baz'])");
@@ -213,15 +215,17 @@
 
    @Test
    public void testActionListenerThatRequiresActionOutputs() throws Exception {
-    write("nobuild/BUILD",
+    write(
+        "nobuild/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name= 'javalib',",
         "             srcs=[])",
         "extra_action(name = 'baz',",
         "             out_templates = ['$(OWNER_LABEL_DIGEST)_$(ACTION_ID).tst'],",
         "             requires_action_output = 1,",
-        "             cmd = " +
-        "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)" +
-        "_$(ACTION_ID).tst)')",
+        "             cmd = "
+            + "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)"
+            + "_$(ACTION_ID).tst)')",
         "action_listener(name = 'bar',",
         "                mnemonics = ['Javac'],",
         "                extra_actions = [':baz'])");
@@ -239,16 +243,18 @@
 
   @Test
   public void testFilteredActionListener() throws Exception {
-    write("filtered/BUILD",
+    write(
+        "filtered/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name= 'a',",
         "             srcs=[])",
         "java_library(name= 'b',",
         "             exports=[':a'])",
         "extra_action(name = 'baz',",
         "             out_templates = ['$(OWNER_LABEL_DIGEST)_$(ACTION_ID).tst'],",
-        "             cmd = " +
-        "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)" +
-            "_$(ACTION_ID).tst)')",
+        "             cmd = "
+            + "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)"
+            + "_$(ACTION_ID).tst)')",
         "action_listener(name = 'bar',",
         "                mnemonics = ['Javac'],",
         "                extra_actions = [':baz'])");
@@ -268,16 +274,18 @@
 
   @Test
   public void testTopLevelOnlyActionListener() throws Exception {
-    write("filtered/BUILD",
+    write(
+        "filtered/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name= 'a',",
         "             srcs=[])",
         "java_library(name= 'b',",
         "             exports=[':a'])",
         "extra_action(name = 'baz',",
         "             out_templates = ['$(OWNER_LABEL_DIGEST)_$(ACTION_ID).tst'],",
-        "             cmd = " +
-        "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)" +
-            "_$(ACTION_ID).tst)')",
+        "             cmd = "
+            + "                 'echo $(EXTRA_ACTION_FILE)>$(output $(OWNER_LABEL_DIGEST)"
+            + "_$(ACTION_ID).tst)')",
         "action_listener(name = 'bar',",
         "                mnemonics = ['Javac'],",
         "                extra_actions = [':baz'])");
@@ -390,13 +398,14 @@
 
   @Test
   public void testActionListenerNotEnabled() throws Exception {
-    write("nobuild/BUILD",
+    write(
+        "nobuild/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name= 'javalib',",
         "             srcs=[])",
         "extra_action(name = 'baz',",
         "             out_templates = ['$(ACTION_ID).tst'],",
-        "             cmd = " +
-            "'echo $(EXTRA_ACTION_FILE)>$(output $(ACTION_ID).tst)')",
+        "             cmd = " + "'echo $(EXTRA_ACTION_FILE)>$(output $(ACTION_ID).tst)')",
         "action_listener(name = 'bar',",
         "                mnemonics = ['Javac'],",
         "                extra_actions = [':baz'])");
@@ -437,6 +446,7 @@
     write(
         "nobuild/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "javalib1",
             srcs = [],
@@ -464,6 +474,7 @@
     write(
         "nobuild/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "javalib",
             srcs = [],
@@ -493,6 +504,7 @@
     write(
         "nobuild/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "javalib",
             srcs = [],
diff --git a/src/test/java/com/google/devtools/build/lib/buildtool/MiscAnalysisTest.java b/src/test/java/com/google/devtools/build/lib/buildtool/MiscAnalysisTest.java
index 9376b3e..dc8d9ef 100644
--- a/src/test/java/com/google/devtools/build/lib/buildtool/MiscAnalysisTest.java
+++ b/src/test/java/com/google/devtools/build/lib/buildtool/MiscAnalysisTest.java
@@ -190,11 +190,13 @@
 
   @Test
   public void testBuildAllAndParsingError() throws Exception {
-    write("pkg/BUILD",
-          "java_binary(",
-          "name = \"foo\",",
-          "  syntax error here",
-          ")");
+    write(
+        "pkg/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
+        "java_binary(",
+        "name = \"foo\",",
+        "  syntax error here",
+        ")");
 
     addOptions("--nobuild");
 
@@ -268,6 +270,7 @@
     write(
         "pkg/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "foo",
             srcs = unknown_value,
diff --git a/src/test/java/com/google/devtools/build/lib/generatedprojecttest/BuildFileContentsGeneratorTest.java b/src/test/java/com/google/devtools/build/lib/generatedprojecttest/BuildFileContentsGeneratorTest.java
index 28996ba..2d0fa17 100644
--- a/src/test/java/com/google/devtools/build/lib/generatedprojecttest/BuildFileContentsGeneratorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/generatedprojecttest/BuildFileContentsGeneratorTest.java
@@ -51,7 +51,7 @@
 
   @Test
   public void defaultPackageVisibilityIsAddedToStartOfBuildFile() throws IllegalStateException {
-    generator.addRule(new BuildRuleBuilder("java_library", generator.uniqueRuleName()));
+    generator.addRule(new BuildRuleBuilder("cc_library", generator.uniqueRuleName()));
     generator.setDefaultPackageVisibility("//visibility:private");
     assertThat(generator.getContents())
         .startsWith("package(default_visibility = ['//visibility:private'])");
@@ -59,7 +59,7 @@
 
   @Test
   public void defaultPackageVisibilityDefaultsToPublic() throws IllegalStateException {
-    generator.addRule(new BuildRuleBuilder("java_library", generator.uniqueRuleName()));
+    generator.addRule(new BuildRuleBuilder("cc_library", generator.uniqueRuleName()));
     assertThat(generator.getContents())
         .startsWith("package(default_visibility = ['//visibility:public'])");
   }
diff --git a/src/test/java/com/google/devtools/build/lib/packages/ExternalPackageTest.java b/src/test/java/com/google/devtools/build/lib/packages/ExternalPackageTest.java
index 2a50e0b..43dd50c 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/ExternalPackageTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/ExternalPackageTest.java
@@ -86,6 +86,7 @@
         ")");
     FileSystemUtils.writeIsoLatin1(
         rootDirectory.getRelative("BUILD"),
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "config_setting(",
         "    name = 'setting',",
         "    values = {'define': 'foo=bar'},",
diff --git a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
index 8dd5525..42de75e 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
@@ -469,7 +469,7 @@
     scratch.file(
         "x/BUILD",
         """
-        java_test(name='j', tags = ['nodeployjar'])
+        sh_test(name='s', srcs = ['foo.sh'])
         test_suite(name='t1')
         test_suite(name='t2', tests=[])
         test_suite(name='t3', tests=['//foo'])
@@ -480,15 +480,15 @@
 
     // Things to note:
     // - The '$implicit_tests' attribute is unset unless the 'tests' attribute is unset or empty.
-    // - The '$implicit_tests' attribute's value for t1 and t2 is magically able to contain both j
+    // - The '$implicit_tests' attribute's value for t1 and t2 is magically able to contain both s
     //    and c, even though c is instantiated after t1 and t2 are.
 
     assertThat(attributes(pkg.getRule("t1")).get("$implicit_tests", BuildType.LABEL_LIST))
         .containsExactlyElementsIn(
-            Sets.newHashSet(Label.parseCanonical("//x:c"), Label.parseCanonical("//x:j")));
+            Sets.newHashSet(Label.parseCanonical("//x:c"), Label.parseCanonical("//x:s")));
     assertThat(attributes(pkg.getRule("t2")).get("$implicit_tests", BuildType.LABEL_LIST))
         .containsExactlyElementsIn(
-            Sets.newHashSet(Label.parseCanonical("//x:c"), Label.parseCanonical("//x:j")));
+            Sets.newHashSet(Label.parseCanonical("//x:c"), Label.parseCanonical("//x:s")));
     assertThat(attributes(pkg.getRule("t3")).get("$implicit_tests", BuildType.LABEL_LIST))
         .isEmpty();
     assertThat(attributes(pkg.getRule("t4")).get("$implicit_tests", BuildType.LABEL_LIST))
diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
index c953598..eea00ba 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
@@ -86,6 +86,7 @@
       config.create(
           "java/com/google/io/protocol/BUILD",
           """
+          load("@rules_java//java:defs.bzl", "java_import")
           package(default_visibility = ["//visibility:public"])
 
           java_import(
@@ -96,6 +97,7 @@
       config.create(
           "java/com/google/io/protocol2/BUILD",
           """
+          load("@rules_java//java:defs.bzl", "java_import")
           package(default_visibility = ["//visibility:public"])
 
           java_import(
@@ -114,6 +116,7 @@
       config.create(
           "java/com/google/io/protocol/BUILD",
           """
+          load("@rules_java//java:defs.bzl", "java_library")
           package(default_visibility = ["//visibility:public"])
 
           java_library(
@@ -125,6 +128,7 @@
       config.create(
           "java/com/google/io/protocol2/BUILD",
           """
+          load("@rules_java//java:defs.bzl", "java_library")
           package(default_visibility = ["//visibility:public"])
 
           java_library(
@@ -249,6 +253,7 @@
     config.create(
         "java/com/google/net/rpc/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         package(default_visibility = ["//visibility:public"])
 
         java_library(
@@ -264,6 +269,7 @@
     config.create(
         "java/com/google/net/rpc3/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         package(default_visibility = ["//visibility:public"])
 
         java_library(
@@ -332,6 +338,7 @@
     config.create(
         "third_party/java/jsr250_annotations/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         package(default_visibility = ["//visibility:public"])
 
         licenses(["notice"])
diff --git a/src/test/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryTest.java b/src/test/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryTest.java
index b33b73b..cbcbf58 100644
--- a/src/test/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryTest.java
@@ -65,6 +65,7 @@
     writeFile(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "my_java",
             srcs = ["foo.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQuerySemanticsTest.java b/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQuerySemanticsTest.java
index 55d2643..b7d0bd6 100644
--- a/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQuerySemanticsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQuerySemanticsTest.java
@@ -413,6 +413,7 @@
     writeFile(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "my_java",
             srcs = ["foo.java"],
@@ -512,6 +513,7 @@
     writeFile(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "my_java",
             srcs = ["foo.java"],
@@ -585,6 +587,7 @@
     writeFile(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "my_java",
             srcs = ["foo.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryTest.java b/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryTest.java
index a7e8910..4b6a0fc 100644
--- a/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryTest.java
@@ -60,6 +60,7 @@
     writeFile(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "my_java",
             srcs = ["foo.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidStarlarkCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidStarlarkCommonTest.java
index aa60d62..ad5c709 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidStarlarkCommonTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidStarlarkCommonTest.java
@@ -58,6 +58,7 @@
     scratch.file(
         "java/android/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         load(":compatible.bzl", "my_rule")
 
         java_plugin(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/filegroup/FilegroupConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/filegroup/FilegroupConfiguredTargetTest.java
index 0408928..0b3ae3c 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/filegroup/FilegroupConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/filegroup/FilegroupConfiguredTargetTest.java
@@ -52,6 +52,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(name  = 'test_app',
             resources = [':data'],
             create_executable = 0,
@@ -170,6 +171,7 @@
     scratch.file("pkg/c.java");
     scratch.file(
         "pkg/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='lib_a', srcs=['a.java'])",
         "java_library(name='lib_b', srcs=['b.java'], deps = [':lib_c'])",
         "java_library(name='lib_c', srcs=['c.java'])",
@@ -189,6 +191,7 @@
     scratch.file("pkg/c.java");
     scratch.file(
         "pkg/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name='lib_a', srcs=['a.java'])",
         "java_library(name='lib_b', srcs=['b.java'], deps = [':lib_c'])",
         "java_library(name='lib_c', srcs=['c.java'])",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java
index 6d3f9b1..5a35b01 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaCompileActionBuilderTest.java
@@ -38,6 +38,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "a",
             srcs = ["a.java"],
@@ -61,6 +62,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "a",
             srcs = [
@@ -80,6 +82,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "a",
             srcs = [
@@ -100,6 +103,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_plugin(
             name = "foo",
             srcs = ["Foo.java"],
@@ -138,6 +142,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "a",
             srcs = ["A.java"],
@@ -153,6 +158,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "a",
             srcs = ["A.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoCodecTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoCodecTest.java
index 308ffd5..b098b1a 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoCodecTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoCodecTest.java
@@ -44,6 +44,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "a",
             srcs = ["a.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoRoundtripTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoRoundtripTest.java
index 0edc7aa..ac2e4ac 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoRoundtripTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoRoundtripTest.java
@@ -71,6 +71,7 @@
     scratch.file(
         "foo/construct_javainfo.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo")
         def _impl(ctx):
             OUTS = {
                 "lib": "lib%s.jar",
@@ -201,7 +202,10 @@
 
   @Test
   public void dictFromJavaInfo_nonEmpty() throws Exception {
-    scratch.overwriteFile("foo/BUILD", "java_library(name = 'java_lib', srcs = ['A.java'])");
+    scratch.overwriteFile(
+        "foo/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'java_lib', srcs = ['A.java'])");
 
     Dict<Object, Object> javaInfo = getDictFromJavaInfo("foo", "java_lib");
 
@@ -211,10 +215,16 @@
   @Test
   public void dictFromJavaInfo_detectsDifference() throws Exception {
 
-    scratch.overwriteFile("foo/BUILD", "java_library(name = 'java_lib', srcs = ['A.java'])");
+    scratch.overwriteFile(
+        "foo/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'java_lib', srcs = ['A.java'])");
     Dict<Object, Object> javaInfoA = getDictFromJavaInfo("foo", "java_lib");
 
-    scratch.overwriteFile("foo/BUILD", "java_library(name = 'java_lib2', srcs = ['A.java'])");
+    scratch.overwriteFile(
+        "foo/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'java_lib2', srcs = ['A.java'])");
     Dict<Object, Object> javaInfoB = getDictFromJavaInfo("foo", "java_lib2");
 
     assertThat((Map<?, ?>) javaInfoA).isNotEqualTo(javaInfoB);
@@ -223,7 +233,10 @@
   @Test
   public void roundtripJavainfo_srcs() throws Exception {
 
-    scratch.overwriteFile("foo/BUILD", "java_library(name = 'java_lib', srcs = ['A.java'])");
+    scratch.overwriteFile(
+        "foo/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'java_lib', srcs = ['A.java'])");
     Dict<Object, Object> javaInfoA = getDictFromJavaInfo("foo", "java_lib");
     scratch.overwriteFile(
         "foo/BUILD",
@@ -243,11 +256,15 @@
 
   @Test
   public void roundtripJavaInfo_deps() throws Exception {
-    scratch.file("bar/BUILD", "java_library(name = 'javalib', srcs = ['A.java'])");
+    scratch.file(
+        "bar/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'javalib', srcs = ['A.java'])");
 
     scratch.overwriteFile(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "java_lib",
             srcs = ["A.java"],
@@ -274,11 +291,15 @@
 
   @Test
   public void roundtipJavaInfo_runtimeDeps() throws Exception {
-    scratch.file("bar/BUILD", "java_library(name = 'deplib', srcs = ['A.java'])");
+    scratch.file(
+        "bar/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'deplib', srcs = ['A.java'])");
 
     scratch.overwriteFile(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "java_lib",
             srcs = ["A.java"],
@@ -305,11 +326,15 @@
 
   @Test
   public void roundtipJavaInfo_exports() throws Exception {
-    scratch.file("bar/BUILD", "java_library(name = 'exportlib', srcs = ['A.java'])");
+    scratch.file(
+        "bar/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'exportlib', srcs = ['A.java'])");
 
     scratch.overwriteFile(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "java_lib",
             srcs = ["A.java"],
@@ -338,11 +363,13 @@
   public void roundtipJavaInfo_plugin() throws Exception {
     scratch.file(
         "bar/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_plugin')",
         "java_plugin(name = 'plugin', srcs = ['A.java'], processor_class = 'bar.Main')");
 
     scratch.overwriteFile(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "java_lib",
             srcs = ["A.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java
index ded7a15..f20c873 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaInfoStarlarkApiTest.java
@@ -155,6 +155,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -190,6 +191,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -257,6 +259,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -420,6 +423,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -452,6 +456,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -484,6 +489,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -526,6 +532,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -568,6 +575,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -610,6 +618,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -681,6 +690,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -757,6 +767,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -794,6 +805,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -855,6 +867,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -894,6 +907,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -935,6 +949,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -966,6 +981,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1001,6 +1017,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1036,6 +1053,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1071,6 +1089,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1136,6 +1155,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1229,6 +1249,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(name = "lib")
@@ -1513,6 +1534,7 @@
       assertThat(useIJar && stampJar).isFalse();
       ImmutableList.Builder<String> lines = ImmutableList.builder();
       lines.add(
+          "load('@rules_java//java:defs.bzl', 'java_common')",
           "result = provider()",
           "def _impl(ctx):",
           "  ctx.actions.write(ctx.outputs.output_jar, 'JavaInfo API Test', is_executable=False) ",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java
index 5bcca72..d136c5a 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaPluginsFlagAliasTest.java
@@ -51,7 +51,10 @@
   /** Tests that a single plugin passed by a flag is returned by java_plugins_flag_alias. */
   @Test
   public void javaPluginFlagAlias_flagWithSinglePlugin() throws Exception {
-    scratch.file("java/BUILD", "java_plugin(name = 'plugin', srcs = ['A.java'])");
+    scratch.file(
+        "java/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_plugin')",
+        "java_plugin(name = 'plugin', srcs = ['A.java'])");
     useConfiguration("--plugin=//java:plugin");
 
     ConfiguredTarget target =
@@ -67,6 +70,7 @@
     scratch.file(
         "java/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         java_plugin(
             name = "plugin1",
             srcs = ["A.java"],
@@ -89,7 +93,10 @@
   /** Tests that passing a java_library to --plugin flag fails. */
   @Test
   public void javaPluginFlagAlias_flagWithJavaLibraryFails() throws Exception {
-    scratch.file("java/BUILD", "java_library(name = 'lib', srcs = ['A.java'])");
+    scratch.file(
+        "java/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'lib', srcs = ['A.java'])");
     useConfiguration("--plugin=//java:lib");
 
     checkError(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaRuntimeTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaRuntimeTest.java
index 5de1e01..e4680c5 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaRuntimeTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaRuntimeTest.java
@@ -42,6 +42,7 @@
     scratch.file(
         "jvm/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_runtime")
         java_runtime(
             name = "jvm-k8",
             srcs = [
@@ -72,7 +73,9 @@
 
   @Test
   public void absoluteJavaHomeWithSrcs() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[':dummy'], java_home='/absolute/path')");
     reporter.removeHandler(failFastHandler);
     getConfiguredTarget("//a:jvm");
@@ -82,7 +85,9 @@
   @Test
   public void absoluteJavaHomeWithJava() throws Exception {
     scratch.file(
-        "a/BUILD", "java_runtime(name='jvm', java='bin/java', java_home='/absolute/path')");
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
+        "java_runtime(name='jvm', java='bin/java', java_home='/absolute/path')");
     reporter.removeHandler(failFastHandler);
     getConfiguredTarget("//a:jvm");
     assertContainsEvent("'java_home' with an absolute path requires 'java' to be empty.");
@@ -90,7 +95,10 @@
 
   @Test
   public void binJavaPathName() throws Exception {
-    scratch.file("BUILD", "java_runtime(name='jvm', java='java')");
+    scratch.file(
+        "BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
+        "java_runtime(name='jvm', java='java')");
     reporter.removeHandler(failFastHandler);
     getConfiguredTarget("//:jvm");
     assertContainsEvent("the path to 'java' must end in 'bin/java'.");
@@ -98,7 +106,9 @@
 
   @Test
   public void absoluteJavaHome() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[], java_home='/absolute/path')");
     reporter.removeHandler(failFastHandler);
     ConfiguredTarget jvm = getConfiguredTarget("//a:jvm");
@@ -107,7 +117,9 @@
 
   @Test
   public void relativeJavaHome() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[], java_home='b/c')");
     reporter.removeHandler(failFastHandler);
     ConfiguredTarget jvm = getConfiguredTarget("//a:jvm");
@@ -130,7 +142,9 @@
 
   @Test
   public void javaHomeWithMakeVariables() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[], java_home='/opt/$(CMDLINE)')");
     useConfiguration("--define=CMDLINE=foo/bar");
     ConfiguredTarget jvm = getConfiguredTarget("//a:jvm");
@@ -139,7 +153,9 @@
 
   @Test
   public void javaHomeWithInvalidMakeVariables() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[], java_home='/opt/$(WTF)')");
     reporter.removeHandler(failFastHandler);
     getConfiguredTarget("//a:jvm");
@@ -148,7 +164,9 @@
 
   @Test
   public void makeVariables() throws Exception {
-    scratch.file("a/BUILD",
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "java_runtime(name='jvm', srcs=[], java_home='/foo/bar')");
     ImmutableMap<String, String> runtime = getConfiguredTarget("//a:jvm")
         .get(TemplateVariableInfo.PROVIDER).getVariables();
@@ -158,7 +176,10 @@
 
   @Test
   public void noSrcs() throws Exception {
-    scratch.file("a/BUILD", "java_runtime(name='jvm', java_home='/opt/jvm')");
+    scratch.file(
+        "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
+        "java_runtime(name='jvm', java_home='/opt/jvm')");
     ConfiguredTarget jvm = getConfiguredTarget("//a:jvm");
     JavaRuntimeInfo provider = getJavaRuntimeInfo(jvm);
     assertThat(provider.javaHome()).isEqualTo("/opt/jvm");
@@ -169,6 +190,7 @@
   public void invalidJavaBase() throws Exception {
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_binary')",
         "java_binary(name='a', srcs=['A.java'])",
         "filegroup(name='fg')",
         "toolchain(",
@@ -189,6 +211,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_runtime")
         genrule(
             name = "gen",
             outs = ["generated_java_home/bin/java"],
@@ -211,6 +234,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_runtime")
         genrule(
             name = "gen",
             outs = ["generated_java_home/bin/java"],
@@ -313,6 +337,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         load(":defs.bzl", "r")
 
         java_binary(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
index d3c7d95..c18590a 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java
@@ -115,6 +115,7 @@
   public void testJavaRuntimeProviderJavaAbsolute() throws Exception {
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -164,6 +165,7 @@
   public void testJavaRuntimeProviderJavaHermetic() throws Exception {
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -213,6 +215,7 @@
   public void testJavaRuntimeProviderJavaGenerated() throws Exception {
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -267,6 +270,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -338,6 +342,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -527,6 +532,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -622,6 +628,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  return java_common.compile(",
@@ -663,6 +670,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  return java_common.compile(",
@@ -688,6 +696,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_library(
@@ -714,6 +723,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -729,6 +739,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  deps = [dep[java_common.provider] for dep in ctx.attr.deps]",
@@ -780,6 +791,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -795,6 +807,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  deps = [dep[java_common.provider] for dep in ctx.attr.deps]",
@@ -844,6 +857,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -859,6 +873,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('amazing.jar')",
         "  other_output_jar = ctx.actions.declare_file('wonderful.jar')",
@@ -925,6 +940,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -980,6 +996,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -1031,6 +1048,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  output_source_jar = ctx.actions.declare_file('lib' + ctx.label.name + '-mysrc.jar')",
@@ -1090,6 +1108,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -1173,6 +1192,7 @@
         """);
     scratch.file(
         "java/test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library', 'java_plugin')",
         "load(':extension.bzl', 'my_rule')",
         "java_library(name = 'plugin_dep',",
         "    srcs = [ 'ProcessorDep.java'])",
@@ -1273,6 +1293,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_library(
             name = "plugin_dep",
             srcs = ["ProcessorDep.java"],
@@ -1309,6 +1330,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_library(
             name = "plugin_dep",
             srcs = ["ProcessorDep.java"],
@@ -1347,6 +1369,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_library(
             name = "plugin_dep1",
             srcs = ["A.java"],
@@ -1439,6 +1462,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":myplugin.bzl", "myplugin")
 
         java_library(
@@ -1503,6 +1527,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":myplugin.bzl", "myplugin")
 
         java_library(
@@ -1564,6 +1589,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":myplugin.bzl", "myplugin")
 
         java_library(
@@ -1626,6 +1652,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":myplugin.bzl", "myplugin")
 
         java_library(
@@ -1684,6 +1711,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1755,6 +1783,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1796,6 +1825,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1849,6 +1879,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1935,6 +1966,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_binary")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -1994,6 +2026,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2187,6 +2220,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2235,6 +2269,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2292,6 +2327,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2349,6 +2385,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2405,6 +2442,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         cc_library(
@@ -2473,6 +2511,7 @@
     scratch.file(
         "foo/extension.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
         def _impl(ctx):
             dep_params = ctx.attr.dep[JavaInfo]
             cc_dep_params = ctx.attr.cc_dep[CcInfo]
@@ -2488,6 +2527,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         cc_library(
@@ -2534,6 +2574,7 @@
     scratch.file(
         "foo/extension.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
         def _impl(ctx):
             dep_params = ctx.attr.dep[JavaInfo]
             cc_dep_params = ctx.attr.cc_dep[CcInfo]
@@ -2549,6 +2590,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_library")
         load(":extension.bzl", "my_rule")
 
         cc_binary(
@@ -2601,6 +2643,7 @@
     scratch.file(
         "foo/extension.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
         def _impl(ctx):
             dep_params = ctx.attr.dep[JavaInfo]
             cc_dep_params = ctx.attr.cc_dep[CcInfo]
@@ -2616,6 +2659,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_test")
         load(":extension.bzl", "my_rule")
 
         cc_binary(
@@ -2677,6 +2721,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         cc_binary(
@@ -2724,6 +2769,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -2828,6 +2874,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2873,6 +2920,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":extension.bzl", "my_rule")
 
         java_library(
@@ -2920,6 +2968,7 @@
     scratch.file(
         "foo/BUILD",
         "load(':extension.bzl', 'my_rule')",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name = 'my_java_lib_a', srcs = ['java/A.java'], javacopts = ['opt1',"
             + " 'opt2'])",
         "my_rule(name = 'my_starlark_rule', dep = ':my_java_lib_a')");
@@ -2946,6 +2995,7 @@
         "java/test/BUILD",
         """
         load(":custom_rule.bzl", "java_custom_library")
+        load("@rules_java//java:defs.bzl", "java_binary")
 
         java_binary(
             name = "plugin",
@@ -2968,6 +3018,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  deps = [dep[java_common.provider] for dep in ctx.attr.deps]",
@@ -3026,6 +3077,7 @@
     scratch.file(
         "java/test/custom_rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo")
         def _impl(ctx):
             output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
             ctx.actions.write(output_jar, "")
@@ -3064,6 +3116,7 @@
     scratch.file(
         "java/test/custom_rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
         def _impl(ctx):
             output_jar = ctx.actions.declare_file("lib" + ctx.label.name + ".jar")
             ctx.actions.write(output_jar, "")
@@ -3109,6 +3162,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  java_info = java_common.compile(",
@@ -3148,6 +3202,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -3193,6 +3248,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -3259,6 +3315,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -3299,6 +3356,7 @@
     scratch.file(
         "foo/custom_library.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         def _impl(ctx):
             java_provider = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
             if not ctx.attr.strict_deps:
@@ -3316,6 +3374,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_library.bzl", "custom_library")
 
         custom_library(
@@ -3349,6 +3408,7 @@
     scratch.file(
         "foo/custom_library.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         def _impl(ctx):
             java_provider = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
             if not ctx.attr.strict_deps:
@@ -3366,6 +3426,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_library.bzl", "custom_library")
 
         custom_library(
@@ -3461,6 +3522,7 @@
     scratch.file(
         "foo/custom_library.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         def _impl(ctx):
             java_provider = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
             return [java_provider]
@@ -3477,6 +3539,7 @@
         "foo/BUILD",
         """
         load(":custom_library.bzl", "custom_library")
+        load("@rules_java//java:defs.bzl", "java_library")
 
         custom_library(
             name = "custom",
@@ -3621,6 +3684,7 @@
     scratch.file(
         "java/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_rule.bzl", "java_custom_library")
 
         java_custom_library(
@@ -3636,6 +3700,7 @@
         """);
     scratch.file(
         "java/test/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('amazing.jar')",
         "  exports = [export[java_common.provider] for export in ctx.attr.exports]",
@@ -3681,6 +3746,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/java_custom_library.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib%s.jar' % ctx.label.name)",
         "  compilation_provider = java_common.compile(",
@@ -3723,6 +3789,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/java_custom_library.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib%s.jar' % ctx.label.name)",
         "  deps = [deps[JavaInfo] for deps in ctx.attr.deps]",
@@ -3747,6 +3814,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":java_custom_library.bzl", "java_custom_library")
 
         java_library(
@@ -3777,6 +3845,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/java_custom_library.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib%s.jar' % ctx.label.name)",
         "  exports = [export[JavaInfo] for export in ctx.attr.exports]",
@@ -3801,6 +3870,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":java_custom_library.bzl", "java_custom_library")
 
         java_library(
@@ -3835,6 +3905,7 @@
     scratch.file(
         "a/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_runtime")
         load(":rule.bzl", "jrule")
 
         java_runtime(
@@ -3851,6 +3922,7 @@
 
     scratch.file(
         "a/rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  java_common.compile(",
@@ -3883,6 +3955,7 @@
     scratch.file(
         "a/rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         load("//myinfo:myinfo.bzl", "MyInfo")
 
         def _impl(ctx):
@@ -3920,6 +3993,7 @@
     scratch.file(
         "a/rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         load("//myinfo:myinfo.bzl", "MyInfo")
 
         def _impl(ctx):
@@ -3958,6 +4032,7 @@
     scratch.file(
         "a/rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         load("//myinfo:myinfo.bzl", "MyInfo")
 
         def _impl(ctx):
@@ -3994,6 +4069,7 @@
     scratch.file("a/a.txt", "hello");
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -4034,6 +4110,7 @@
     scratch.file(
         "java/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "lib",
             resources = [":libjni.so"],
@@ -4058,6 +4135,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  output_jar = ctx.actions.declare_file('lib' + ctx.label.name + '.jar')",
         "  compilation_provider = java_common.compile(",
@@ -4088,6 +4166,7 @@
         "foo/BUILD",
         """
         load(":custom_rule.bzl", "java_custom_library")
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
 
         java_plugin(
             name = "processor",
@@ -4146,6 +4225,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common.compile(",
         "    ctx,",
@@ -4183,6 +4263,7 @@
     scratch.file("foo/resource.txt", "Totally real resource content");
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common.compile(",
         "    ctx,",
@@ -4223,6 +4304,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common.compile(",
         "    ctx,",
@@ -4259,6 +4341,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common.compile(",
         "    ctx,",
@@ -4295,6 +4378,7 @@
     scratch.file(
         "foo/custom_rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common", "JavaInfo")
         def _impl(ctx):
             output_jar = ctx.actions.declare_file("lib.jar")
             java_info = JavaInfo(output_jar = output_jar, compile_jar = None)
@@ -4328,6 +4412,7 @@
     scratch.file(
         "foo/custom_rule.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common", "JavaInfo")
         def _impl(ctx):
             output_jar = ctx.actions.declare_file("lib.jar")
             java_info = JavaInfo(output_jar = output_jar, compile_jar = None)
@@ -4361,6 +4446,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common.compile(",
         "    ctx,",
@@ -4397,6 +4483,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
         "  java_common.compile(",
@@ -4464,6 +4551,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
         "  java_common.run_ijar(",
@@ -4500,6 +4588,7 @@
     JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  out = ctx.actions.declare_file('output.jar')",
         "  java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
@@ -4558,6 +4647,7 @@
     scratch.file(
         "foo/custom_library.bzl",
         """
+        load("@rules_java//java:defs.bzl", "java_common")
         def _impl(ctx):
             java_provider = java_common.merge([dep[JavaInfo] for dep in ctx.attr.deps])
             return [java_provider]
@@ -4572,6 +4662,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(":custom_library.bzl", "custom_library")
 
         custom_library(
@@ -4601,6 +4692,7 @@
     scratch.file("a/libStatic.a");
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -4651,6 +4743,7 @@
     scratch.file("a/libStatic.a");
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -4699,6 +4792,7 @@
     scratch.file("a/libStatic.a");
     scratch.file(
         "a/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':rule.bzl', 'jrule')",
         "load('"
             + TestConstants.TOOLS_REPOSITORY
@@ -4806,6 +4900,7 @@
                 () -> new IllegalArgumentException("API not declared on java_common: " + api));
     scratch.file(
         "foo/custom_rule.bzl",
+        "load('@rules_java//java:defs.bzl', 'java_common')",
         "def _impl(ctx):",
         "  java_common." + api + "()",
         "  return []",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaTestUtil.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaTestUtil.java
index cc7d267..f82da4c9 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaTestUtil.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaTestUtil.java
@@ -41,6 +41,8 @@
     scratch.file("java/com/google/test/tzdata.jar");
     scratch.overwriteFile(
         "java/com/google/test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime',"
+            + " 'java_toolchain')",
         "java_toolchain(name = 'toolchain',",
         "    source_version = '6',",
         "    target_version = '6',",
diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/proto/StarlarkJavaLiteProtoLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/proto/StarlarkJavaLiteProtoLibraryTest.java
index dfc34e4..2eab718 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/java/proto/StarlarkJavaLiteProtoLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/java/proto/StarlarkJavaLiteProtoLibraryTest.java
@@ -97,6 +97,7 @@
     mockToolsConfig.overwrite(
         "protobuf/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         package(default_visibility = ["//visibility:public"])
 
         java_library(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/objc/XcodeConfigTest.java b/src/test/java/com/google/devtools/build/lib/rules/objc/XcodeConfigTest.java
index 3b3f003..85c2d59 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/objc/XcodeConfigTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/objc/XcodeConfigTest.java
@@ -1628,6 +1628,7 @@
     scratch.file(
         "x/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load("//x:provider_grabber.bzl", "provider_grabber")
 
         xcode_config(
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/analysis/FrontierSerializerTestBase.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/analysis/FrontierSerializerTestBase.java
index ae7c040..d115dc9 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/analysis/FrontierSerializerTestBase.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/analysis/FrontierSerializerTestBase.java
@@ -376,6 +376,7 @@
     write(
         "bar/BUILD",
         """
+load("@rules_java//java:defs.bzl", "java_library")
 java_library(
     name = "one",
     srcs = ["One.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java b/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
index fa32dcc..14146fe 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/BindTest.java
@@ -28,7 +28,6 @@
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
-import java.util.function.Function;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,7 +39,8 @@
 
   @Before
   public final void createFiles() throws Exception {
-    analysisMock.javaSupport().setupRulesJava(mockToolsConfig, Function.identity());
+    Runfiles runfiles = Runfiles.preload().withSourceRepository("");
+    analysisMock.javaSupport().setupRulesJava(mockToolsConfig, runfiles::rlocation);
     setupStarlarkRules(scratch);
     scratch.file(
         "test/BUILD",
@@ -64,6 +64,7 @@
     // mock Java setup.
     scratch.overwriteFile(
         TestConstants.TOOLS_REPOSITORY_SCRATCH + "tools/jdk/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_runtime')",
         "load(':java_toolchain_alias.bzl', 'java_runtime_alias')",
         "package(default_visibility = ['//visibility:public'])",
         "java_runtime_alias(name = 'current_java_runtime')",
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkDefinedAspectsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkDefinedAspectsTest.java
index 093c816..f193459 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkDefinedAspectsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkDefinedAspectsTest.java
@@ -94,7 +94,10 @@
            return []
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
@@ -113,7 +116,10 @@
            return foo()
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
@@ -140,7 +146,10 @@
            return [foo(), bar()]
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
@@ -170,7 +179,10 @@
            return [foo(), bar()]
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
@@ -207,7 +219,10 @@
            return []
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("//test:aspect.bzl%MyAspect"), "//test:xxx");
@@ -233,7 +248,10 @@
         """);
     scratch.file("local/repo/BUILD");
 
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("@local//:aspect.bzl%MyAspect"), "//test:xxx");
@@ -258,7 +276,10 @@
            return []
         MyAspect = aspect(implementation=_impl, fragments=['java'])
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     AnalysisResult analysisResult =
         update(ImmutableList.of("test/aspect.bzl%MyAspect"), "//test:xxx");
@@ -297,6 +318,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -443,6 +465,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'xxx',
              srcs = ['A.java'],
@@ -487,6 +510,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'xxx',
              srcs = ['A.java'],
@@ -551,6 +575,7 @@
         "test/BUILD",
         """
         load('//test:aspect.bzl', 'my_rule')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -594,6 +619,7 @@
         "test/BUILD",
         """
         load('//test:aspect.bzl', 'my_rule')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -634,6 +660,7 @@
         "test/BUILD",
         """
         load('//test:inc.bzl', 'r')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(name = 'j')
         r(name = 'test', a = [':j'])
         """);
@@ -669,6 +696,7 @@
         "test/BUILD",
         """
         load('//test:rule.bzl', 'my_rule')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -721,6 +749,7 @@
         "test/BUILD",
         """
         load('//test:aspect.bzl', 'my_rule')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -765,6 +794,7 @@
         "test/BUILD",
         """
         load('//test:aspect.bzl', 'my_rule')
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'yyy',
         )
@@ -798,6 +828,7 @@
         "test/BUILD",
         """
         exports_files(['zzz.jar'])
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'xxx',
              srcs = ['A.java'],
@@ -828,6 +859,7 @@
         "test/BUILD",
         """
         exports_files(['zzz.jar'])
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
              name = 'xxx',
              srcs = ['A.java'],
@@ -854,7 +886,10 @@
 
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -867,7 +902,7 @@
     // Stack doesn't include source lines because we haven't told EvalException
     // how to read from scratch.
     assertContainsEvent(
-        "ERROR /workspace/test/BUILD:1:13: in "
+        "ERROR /workspace/test/BUILD:2:13: in "
             + "//test:aspect.bzl%MyAspect aspect on java_library rule //test:xxx: \n"
             + "Traceback (most recent call last):\n"
             + "\tFile \"/workspace/test/aspect.bzl\", line 2, column 13, in _impl\n"
@@ -885,7 +920,10 @@
 
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -911,7 +949,10 @@
 
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -922,7 +963,7 @@
       // expect to fail.
     }
     assertContainsEvent(
-        "ERROR /workspace/test/BUILD:1:13: in "
+        "ERROR /workspace/test/BUILD:2:13: in "
             + "//test:aspect.bzl%MyAspect aspect on java_library rule //test:xxx: \n"
             + "The following files have no generating action:\n"
             + "test/missing_in_action.txt");
@@ -1036,7 +1077,10 @@
   @Test
   public void topLevelAspectIsNotAnAspect() throws Exception {
     scratch.file("test/aspect.bzl", "MyAspect = 4");
-    scratch.file("test/BUILD", "java_library(name = 'xxx')");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx')");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -1454,7 +1498,10 @@
   @Test
   public void topLevelAspectDoesNotExist() throws Exception {
     scratch.file("test/aspect.bzl", "");
-    scratch.file("test/BUILD", "java_library(name = 'xxx')");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx')");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -1469,7 +1516,10 @@
 
   @Test
   public void topLevelAspectDoesNotExist2() throws Exception {
-    scratch.file("test/BUILD", "java_library(name = 'xxx')");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx')");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -1484,7 +1534,10 @@
 
   @Test
   public void topLevelAspectDoesNotExistNoBuildFile() throws Exception {
-    scratch.file("test/BUILD", "java_library(name = 'xxx')");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx')");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -2268,6 +2321,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         extra_action(
             name = 'xa',
             cmd = 'echo $(EXTRA_ACTION_FILE) > $(output file.xa)',
@@ -2302,6 +2356,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         extra_action(
             name = 'xa',
             cmd = 'echo $(EXTRA_ACTION_FILE) > $(output file.xa)',
@@ -2351,6 +2406,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = 'tool',
         )
@@ -2699,7 +2755,10 @@
         my_aspect = aspect(_impl, provides = ['foo'])
         a_dict = { 'foo' : attr.label_list(aspects = [my_aspect]) }
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     reporter.removeHandler(failFastHandler);
     try {
@@ -9768,7 +9827,10 @@
            return []
         MyAspect = aspect(implementation=_impl)
         """);
-    scratch.file("test/BUILD", "java_library(name = 'xxx',)");
+    scratch.file(
+        "test/BUILD",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
+        "java_library(name = 'xxx',)");
 
     // Runs a basic analysis to prime test/aspect.bzl in Skyframe.
     AnalysisResult unusedAnalysisResult =
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
index e42e0fd..81372cc 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java
@@ -835,6 +835,7 @@
     scratch.file(
         "test/starlark/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         load('//test/starlark:extension.bzl', 'custom_rule')
 
         custom_rule(name = 'cr')
@@ -947,6 +948,7 @@
     scratch.file(
         "test/starlark/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//test/starlark:extension.bzl', 'custom_rule')
 
         java_library(name='jl', srcs = [':A.java'])
@@ -984,6 +986,7 @@
     scratch.file(
         "test/starlark/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//test/starlark:extension.bzl', 'custom_rule')
 
         java_library(name='jl', srcs = [':A.java'])
@@ -1022,6 +1025,7 @@
     scratch.file(
         "test/starlark/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//test/starlark:extension.bzl', 'custom_rule')
 
         java_library(name='jl', srcs = [':A.java'])
@@ -2664,6 +2668,7 @@
     scratch.file(
         "test/starlark/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//test/starlark:extension.bzl',  'my_rule')
         java_library(name = 'dep', srcs = ['a.java'], restricted_to = ['//buildenv/foo:other'])
         my_rule(name='my', deps = [':dep'])
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java
index 0eb9d84..3af5d26 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleClassFunctionsTest.java
@@ -157,6 +157,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         genrule(
             name = "foo",
             srcs = [
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java
index 68f7d41..372b30d 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java
@@ -122,6 +122,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         package(features = ['-f1', 'f2', 'f3'])
         genrule(name = 'foo',
           cmd = 'dummy_cmd',
@@ -162,6 +163,7 @@
     scratch.file(
         "test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load('//test:macros.bzl', 'macro_native_rule', 'macro_starlark_rule', 'starlark_rule')
         macro_native_rule(name = 'm_native',
           deps = [':jlib'])
@@ -211,7 +213,7 @@
     setUpAttributeErrorTest();
     assertThrows(Exception.class, () -> createRuleContext("//test:m_starlark"));
     assertContainsEvent(
-        "ERROR /workspace/test/BUILD:4:20: in deps attribute of starlark_rule rule "
+        "ERROR /workspace/test/BUILD:5:20: in deps attribute of starlark_rule rule "
             + "//test:m_starlark: '//test:jlib' does not have mandatory providers:"
             + " 'some_provider'. "
             + "Since this rule was created by the macro 'macro_starlark_rule', the error might "
@@ -233,7 +235,7 @@
     setUpAttributeErrorTest();
     assertThrows(Exception.class, () -> createRuleContext("//test:skyrule"));
     assertContainsEvent(
-        "ERROR /workspace/test/BUILD:10:14: in deps attribute of "
+        "ERROR /workspace/test/BUILD:11:14: in deps attribute of "
             + "starlark_rule rule //test:skyrule: '//test:jlib' does not have mandatory providers: "
             + "'some_provider'");
   }
@@ -2117,6 +2119,7 @@
     scratch.file(
         "bar/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         load(':rules.bzl', 'forward_default_info')
         java_library(
             name = 'lib',
diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java
index e765965..7f64d8b 100644
--- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleImplementationFunctionsTest.java
@@ -145,6 +145,7 @@
     scratch.file(
         "foo/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         genrule(name = 'foo',
           cmd = 'dummy_cmd',
           srcs = ['a.txt', 'b.img'],
diff --git a/src/test/java/com/google/devtools/build/lib/view/java/JavaCompileOnlyTest.java b/src/test/java/com/google/devtools/build/lib/view/java/JavaCompileOnlyTest.java
index 6867a2d..d6cac7c 100644
--- a/src/test/java/com/google/devtools/build/lib/view/java/JavaCompileOnlyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/view/java/JavaCompileOnlyTest.java
@@ -36,6 +36,7 @@
     scratch.file(
         "java/main/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary")
         java_binary(
             name = "main",
             srcs = ["Main.java"],
@@ -50,6 +51,7 @@
     scratch.file(
         "java/hello_library/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "hello_library",
             srcs = ["HelloLibrary.java"],
diff --git a/src/test/java/com/google/devtools/build/lib/view/java/JavaImportConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/view/java/JavaImportConfiguredTargetTest.java
index a3de9d0..b324276 100644
--- a/src/test/java/com/google/devtools/build/lib/view/java/JavaImportConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/view/java/JavaImportConfiguredTargetTest.java
@@ -62,6 +62,7 @@
     scratch.file(
         "java/jarlib/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         java_import(
             name = "libraryjar",
             jars = ["library.jar"],
@@ -102,7 +103,12 @@
   // Regression test for b/262751943.
   @Test
   public void testCommandLineContainsTargetLabel() throws Exception {
-    scratch.file("java/BUILD", "java_import(name = 'java_imp', jars = ['import.jar'])");
+    scratch.file(
+        "java/BUILD",
+        """
+        load("@rules_java//java:defs.bzl", "java_import")
+        java_import(name = 'java_imp', jars = ['import.jar'])
+        """);
 
     ConfiguredTarget configuredTarget = getConfiguredTarget("//java:java_imp");
     Artifact compiledArtifact =
@@ -122,6 +128,7 @@
     scratchConfiguredTarget(
         "java",
         "javalib",
+        "load('@rules_java//java:defs.bzl', 'java_library')",
         "java_library(name = 'javalib',",
         "             srcs = ['Other.java'],",
         "             exports = ['//java/jarlib:libraryjar'])");
@@ -155,6 +162,7 @@
     scratch.file(
         "java/somelib/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library")
         java_library(
             name = "javalib",
             srcs = ["Other.java"],
@@ -176,6 +184,7 @@
     scratch.file(
         "java/jarlib2/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         java_library(
             name = "lib",
             srcs = ["Main.java"],
@@ -253,6 +262,7 @@
     scratch.file(
         "java/jarlib2/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         java_library(
             name = "lib",
             srcs = ["Main.java"],
@@ -315,6 +325,7 @@
     scratch.file(
         "java/genrules/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         genrule(
             name = "generated_jar",
             outs = ["generated.jar"],
@@ -357,6 +368,7 @@
     scratch.file(
         "java/srcjarlib/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         java_import(
             name = "library-jar",
             jars = ["somelib.jar"],
@@ -374,13 +386,26 @@
 
   @Test
   public void testRequiresJars() throws Exception {
-    checkError("pkg", "rule", "mandatory attribute 'jars'", "java_import(name = 'rule')");
+    checkError(
+        "pkg",
+        "rule",
+        "mandatory attribute 'jars'",
+        """
+        load("@rules_java//java:defs.bzl", "java_import")
+        java_import(name = 'rule')
+        """);
   }
 
   @Test
   public void testPermitsEmptyJars() throws Exception {
     useConfiguration("--incompatible_disallow_java_import_empty_jars=0");
-    scratchConfiguredTarget("pkg", "rule", "java_import(name = 'rule', jars = [])");
+    scratchConfiguredTarget(
+        "pkg",
+        "rule",
+        """
+        load("@rules_java//java:defs.bzl", "java_import")
+        java_import(name = 'rule', jars = [])
+        """);
     assertNoEvents();
   }
 
@@ -391,7 +416,10 @@
         "pkg",
         "rule",
         "expected no files",
-        "java_import(name = 'rule', jars = ['good.jar'], exports = ['bad.jar'])");
+        """
+        load("@rules_java//java:defs.bzl", "java_import")
+        java_import(name = 'rule', jars = ['good.jar'], exports = ['bad.jar'])
+        """);
   }
 
   @Test
@@ -402,6 +430,7 @@
         "library-jar",
         getErrorMsgMisplacedFiles(
             "jars", "java_import", "//badlib:library-jar", "//badlib:not-a-jar.txt"),
+        "load('@rules_java//java:defs.bzl', 'java_import')",
         "java_import(name = 'library-jar',",
         "            jars = ['not-a-jar.txt'])");
   }
@@ -413,6 +442,7 @@
         "library-jar",
         getErrorMsgNoGoodFiles("jars", "java_import", "//badlib:library-jar", "//badlib:gen"),
         "genrule(name = 'gen', outs = ['not-a-jar.txt'], cmd = '')",
+        "load('@rules_java//java:defs.bzl', 'java_import')",
         "java_import(name  = 'library-jar',",
         "            jars = [':gen'])");
   }
@@ -423,6 +453,7 @@
         "badlib",
         "library-jar",
         "'jars' attribute cannot contain labels of Java targets",
+        "load('@rules_java//java:defs.bzl', 'java_library', 'java_import')",
         "java_library(name = 'javalib',",
         "             srcs = ['Javalib.java'])",
         "java_import(name  = 'library-jar',",
@@ -434,6 +465,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         java_import(
             name = "export",
             constraints = ["android"],
@@ -470,6 +502,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         java_import(
             name = "lib",
             constraints = ["android"],
@@ -496,6 +529,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_import")
         java_import(
             name = "transitive",
             constraints = ["android"],
@@ -529,6 +563,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         java_library(name = "lib")
 
         java_import(
@@ -549,6 +584,8 @@
         scratchConfiguredTarget(
             "java/my",
             "a",
+            "load('@rules_java//java:defs.bzl', 'java_library',"
+                + " 'java_import')",
             "java_import(name = 'a',",
             "    jars = ['dummy.jar'],",
             "    srcjar = 'dummy-src.jar',",
@@ -568,6 +605,7 @@
     scratch.file(
         "java/com/google/exports/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_binary", "java_import")
         java_import(
             name = "other_lib",
             data = ["foo.txt"],
@@ -602,6 +640,7 @@
     scratch.file(
         "java/jarlib2/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         java_library(
             name = "lib",
             srcs = ["Lib.java"],
@@ -636,6 +675,7 @@
     scratch.file(
         "java/com/google/runtimetest/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_import")
         java_import(
             name = "import_dep",
             jars = ["import_compile.jar"],
@@ -678,6 +718,7 @@
         // error:
         "Label '//ji:a.jar' is duplicated in the 'jars' attribute of rule 'ji-with-dupe'",
         // build file
+        "load('@rules_java//java:defs.bzl', 'java_import')",
         "filegroup(name='jars', srcs=['a.jar'])",
         "java_import(name = 'ji-with-dupe', jars = ['a.jar', 'a.jar'])");
   }
@@ -690,6 +731,7 @@
         // error:
         "in jars attribute of java_import rule //ji:ji-with-dupe-through-fg: a.jar is a duplicate",
         // build file
+        "load('@rules_java//java:defs.bzl', 'java_import')",
         "filegroup(name='jars', srcs=['a.jar'])",
         "java_import(name = 'ji-with-dupe-through-fg', jars = ['a.jar', ':jars'])");
   }
@@ -710,6 +752,8 @@
         scratchConfiguredTarget(
             "java/a",
             "a",
+            "load('@rules_java//java:defs.bzl', 'java_import',"
+                + " 'java_library')",
             "java_library(name='a', srcs=['A.java'], deps=[':b'])",
             "java_import(name='b', jars=['b.jar'])");
     List<String> jars =
@@ -727,6 +771,7 @@
         "ugly",
         "jar",
         "java_import.exports is no longer supported; use java_import.deps instead",
+        "load('@rules_java//java:defs.bzl', 'java_import', 'java_library')",
         "java_library(name = 'dep', srcs = ['dep.java'])",
         "java_import(name = 'jar',",
         "    jars = ['dummy.jar'],",
diff --git a/src/test/java/com/google/devtools/build/lib/view/java/JavaPluginConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/view/java/JavaPluginConfiguredTargetTest.java
index 18fb489..14d22d5 100644
--- a/src/test/java/com/google/devtools/build/lib/view/java/JavaPluginConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/view/java/JavaPluginConfiguredTargetTest.java
@@ -44,6 +44,7 @@
         "java/plugin",
         "plugin",
         "no such attribute 'constraints' in 'java_plugin'",
+        "load('@rules_java//java:defs.bzl', 'java_plugin')",
         "java_plugin(name = 'plugin',",
         "            srcs = ['A.java'],",
         "            processor_class = 'xx',",
@@ -54,6 +55,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_library(
             name = "deps",
             srcs = ["Deps.java"],
@@ -132,6 +134,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_plugin(
             name = "plugin",
             srcs = ["Plugin.java"],
@@ -166,6 +169,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         java_plugin(
             name = "plugin",
             srcs = ["Plugin.java"],
@@ -191,6 +195,7 @@
     scratch.file(
         "java/com/google/android/hello/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_library(
             name = "transitive",
             srcs = ["Transitive.java"],
@@ -222,6 +227,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         java_plugin(
             name = "api_generating",
             srcs = ["ApiGeneratingPlugin.java"],
@@ -247,6 +253,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_plugin")
         java_plugin(
             name = "impl_generating",
             srcs = ["ImplGeneratingPlugin.java"],
@@ -272,6 +279,7 @@
     scratch.file(
         "java/com/google/test/BUILD",
         """
+        load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
         java_plugin(
             name = "impl_generating",
             srcs = ["ImplGeneratingPlugin.java"],