Fix java example

Signed-off-by: Adin Cebic <cebic.ad@gmail.com>
diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel
index f43a20c..7c11b8f 100644
--- a/examples/MODULE.bazel
+++ b/examples/MODULE.bazel
@@ -5,6 +5,16 @@
 bazel_dep(name = "aspect_rules_ts", version = "3.5.1")
 bazel_dep(name = "platforms", version = "0.0.11")
 bazel_dep(name = "protobuf", version = "29.3")
+single_version_override(
+    module_name = "protobuf",
+    patch_strip = 1,
+    patches = [
+        "//third_party:protobuf/0001-bazel-Remove-hardcoded-dependency-on-protoc-from-lan.patch",
+        "//third_party:protobuf/0002-Switch-to-toolchains.patch",
+    ],
+    version = "29.3",
+)
+
 bazel_dep(name = "rules_java", version = "8.6.3")
 bazel_dep(name = "rules_proto", version = "7.1.0")
 bazel_dep(name = "rules_python", version = "1.2.0")
diff --git a/examples/third_party/BUILD.bazel b/examples/third_party/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/third_party/BUILD.bazel
diff --git a/examples/third_party/protobuf/0001-bazel-Remove-hardcoded-dependency-on-protoc-from-lan.patch b/examples/third_party/protobuf/0001-bazel-Remove-hardcoded-dependency-on-protoc-from-lan.patch
new file mode 100644
index 0000000..8d28832
--- /dev/null
+++ b/examples/third_party/protobuf/0001-bazel-Remove-hardcoded-dependency-on-protoc-from-lan.patch
@@ -0,0 +1,89 @@
+From 755aafd6fa3de6ea507be6a7e5790c6b53e0e5f9 Mon Sep 17 00:00:00 2001
+From: Fabian Meumertzheim <fabian@meumertzhe.im>
+Date: Mon, 16 Dec 2024 15:55:24 +0100
+Subject: [PATCH 1/2] bazel: Remove hardcoded dependency on `//:protoc` from
+ language runtimes
+
+Without this change, language runtimes still result in a build of `//:protoc` even with a prebuilt `proto_toolchain` registered or `--proto_compiler` set to a precompiled protoc.
+---
+ bazel/private/BUILD |  6 ++++++
+ protobuf.bzl        | 13 +++++--------
+ 2 files changed, 11 insertions(+), 8 deletions(-)
+
+diff --git a/bazel/private/BUILD b/bazel/private/BUILD
+index 8c1c94ac8..a5b3abeda 100644
+--- a/bazel/private/BUILD
++++ b/bazel/private/BUILD
+@@ -1,4 +1,5 @@
+ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
++load(":current_protoc.bzl", "current_protoc")
+ load(":native_bool_flag.bzl", "native_bool_flag")
+ 
+ package(default_applicable_licenses = ["//:license"])
+@@ -28,6 +29,11 @@ toolchain_type(
+     visibility = ["//visibility:public"],
+ )
+ 
++current_protoc(
++    name = "current_protoc",
++    visibility = ["//:__subpackages__"],
++)
++
+ bzl_library(
+     name = "upb_proto_library_internal_bzl",
+     srcs = [
+diff --git a/protobuf.bzl b/protobuf.bzl
+index fdf09bd6b..736cc19cf 100644
+--- a/protobuf.bzl
++++ b/protobuf.bzl
+@@ -2,6 +2,7 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
+ load("@rules_cc//cc:defs.bzl", "objc_library")
+ load("@rules_python//python:defs.bzl", "py_library")
+ load("//bazel/common:proto_info.bzl", "ProtoInfo")
++load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun")
+ 
+ def _GetPath(ctx, path):
+     if ctx.label.workspace_root:
+@@ -310,7 +311,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
+             args.add_all([src.path[offset:] for src in dep.direct_sources])
+ 
+     ctx.actions.run(
+-        executable = ctx.executable._protoc,
++        executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
+         inputs = descriptors,
+         outputs = [srcjar],
+         arguments = [args],
+@@ -335,9 +336,7 @@ internal_gen_well_known_protos_java = rule(
+             default = False,
+         ),
+         "_protoc": attr.label(
+-            executable = True,
+-            cfg = "exec",
+-            default = "//:protoc",
++            default = "//bazel/private:current_protoc",
+         ),
+     },
+ )
+@@ -373,7 +372,7 @@ def _internal_gen_kt_protos(ctx):
+             args.add_all([src.path[offset:] for src in dep.direct_sources])
+ 
+     ctx.actions.run(
+-        executable = ctx.executable._protoc,
++        executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
+         inputs = descriptors,
+         outputs = [srcjar],
+         arguments = [args],
+@@ -398,9 +397,7 @@ internal_gen_kt_protos = rule(
+             default = False,
+         ),
+         "_protoc": attr.label(
+-            executable = True,
+-            cfg = "exec",
+-            default = "//:protoc",
++            default = "//bazel/private:current_protoc",
+         ),
+     },
+ )
+-- 
+2.49.0
+
diff --git a/examples/third_party/protobuf/0002-Switch-to-toolchains.patch b/examples/third_party/protobuf/0002-Switch-to-toolchains.patch
new file mode 100644
index 0000000..8193b8f
--- /dev/null
+++ b/examples/third_party/protobuf/0002-Switch-to-toolchains.patch
@@ -0,0 +1,123 @@
+From d2e4671e04bd72211d15183f891707c4ff2f11cd Mon Sep 17 00:00:00 2001
+From: Fabian Meumertzheim <fabian@meumertzhe.im>
+Date: Mon, 16 Dec 2024 22:20:13 +0100
+Subject: [PATCH 2/2] Switch to toolchains
+
+---
+ bazel/private/BUILD |  6 ------
+ protobuf.bzl        | 41 ++++++++++++++++++++++++++++++-----------
+ 2 files changed, 30 insertions(+), 17 deletions(-)
+
+diff --git a/bazel/private/BUILD b/bazel/private/BUILD
+index a5b3abeda..8c1c94ac8 100644
+--- a/bazel/private/BUILD
++++ b/bazel/private/BUILD
+@@ -1,5 +1,4 @@
+ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+-load(":current_protoc.bzl", "current_protoc")
+ load(":native_bool_flag.bzl", "native_bool_flag")
+ 
+ package(default_applicable_licenses = ["//:license"])
+@@ -29,11 +28,6 @@ toolchain_type(
+     visibility = ["//visibility:public"],
+ )
+ 
+-current_protoc(
+-    name = "current_protoc",
+-    visibility = ["//:__subpackages__"],
+-)
+-
+ bzl_library(
+     name = "upb_proto_library_internal_bzl",
+     srcs = [
+diff --git a/protobuf.bzl b/protobuf.bzl
+index 736cc19cf..acb190942 100644
+--- a/protobuf.bzl
++++ b/protobuf.bzl
+@@ -1,8 +1,9 @@
+ load("@bazel_skylib//lib:versions.bzl", "versions")
+ load("@rules_cc//cc:defs.bzl", "objc_library")
+ load("@rules_python//python:defs.bzl", "py_library")
++load("//bazel/common:proto_common.bzl", "proto_common")
+ load("//bazel/common:proto_info.bzl", "ProtoInfo")
+-load("//bazel/private:current_protoc.bzl", "ProtocFilesToRun")
++load("//bazel/private:toolchain_helpers.bzl", "toolchains")
+ 
+ def _GetPath(ctx, path):
+     if ctx.label.workspace_root:
+@@ -72,6 +73,26 @@ def _CsharpOuts(srcs):
+         for src in srcs
+     ]
+ 
++_PROTOC_ATTRS = toolchains.if_legacy_toolchain({
++    "_proto_compiler": attr.label(
++        cfg = "exec",
++        executable = True,
++        allow_files = True,
++        default = configuration_field("proto", "proto_compiler"),
++    ),
++})
++_PROTOC_FRAGMENTS = ["proto"]
++_PROTOC_TOOLCHAINS = toolchains.use_toolchain(toolchains.PROTO_TOOLCHAIN)
++
++def _protoc_files_to_run(ctx):
++    if proto_common.INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION:
++        toolchain = ctx.toolchains[toolchains.PROTO_TOOLCHAIN]
++        if not toolchain:
++            fail("Protocol compiler toolchain could not be resolved.")
++        return toolchain.proto.proto_compiler
++    else:
++        return ctx.attr._proto_compiler[DefaultInfo].files_to_run
++
+ ProtoGenInfo = provider(
+     fields = ["srcs", "import_flags", "deps"],
+ )
+@@ -311,7 +332,7 @@ def _internal_gen_well_known_protos_java_impl(ctx):
+             args.add_all([src.path[offset:] for src in dep.direct_sources])
+ 
+     ctx.actions.run(
+-        executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
++        executable = _protoc_files_to_run(ctx),
+         inputs = descriptors,
+         outputs = [srcjar],
+         arguments = [args],
+@@ -335,10 +356,9 @@ internal_gen_well_known_protos_java = rule(
+         "javalite": attr.bool(
+             default = False,
+         ),
+-        "_protoc": attr.label(
+-            default = "//bazel/private:current_protoc",
+-        ),
+-    },
++    } | _PROTOC_ATTRS,
++    fragments = _PROTOC_FRAGMENTS,
++    toolchains = _PROTOC_TOOLCHAINS,
+ )
+ 
+ def _internal_gen_kt_protos(ctx):
+@@ -372,7 +392,7 @@ def _internal_gen_kt_protos(ctx):
+             args.add_all([src.path[offset:] for src in dep.direct_sources])
+ 
+     ctx.actions.run(
+-        executable = ctx.attr._protoc[ProtocFilesToRun].files_to_run,
++        executable = _protoc_files_to_run(ctx),
+         inputs = descriptors,
+         outputs = [srcjar],
+         arguments = [args],
+@@ -396,10 +416,9 @@ internal_gen_kt_protos = rule(
+         "lite": attr.bool(
+             default = False,
+         ),
+-        "_protoc": attr.label(
+-            default = "//bazel/private:current_protoc",
+-        ),
+-    },
++    } | _PROTOC_ATTRS,
++    fragments = _PROTOC_FRAGMENTS,
++    toolchains = _PROTOC_TOOLCHAINS,
+ )
+ 
+ def internal_objc_proto_library(
+-- 
+2.49.0
+