Search code examples
javamavenjavafxbazel

How can I run a JavaFX program using Bazel?


I currently have a very trivial JavaFX "Hello, World!" application that I am trying to build and run with Bazel. I am using the maven_install() rule to install the JavaFX dependencies in my WORKSPACE files like so:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "3.2"
RULES_JVM_EXTERNAL_SHA = "82262ff4223c5fda6fb7ff8bd63db8131b51b413d26eb49e3131037e79e324af"

http_archive(
    name = "rules_jvm_external",
    strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
    sha256 = RULES_JVM_EXTERNAL_SHA,
    url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
    artifacts = [
        "org.openjfx:javafx-controls:mac:11.0.1",
        "org.openjfx:javafx-graphics:mac:11.0.1",
        "org.openjfx:javafx-base:mac:11.0.1",
    ],
    repositories = [
        "https://repo1.maven.org/maven2",
    ],
)

And then I try to build a java_binary in the BUILD file like so:

java_binary(
    name = "app",
    srcs = glob(["src/**/*.java"]),
    main_class = "com.dylanpowers.Main",
    deps = [
        "@maven//:org_openjfx_javafx_controls_mac",
        "@maven//:org_openjfx_javafx_graphics_mac",
        "@maven//:org_openjfx_javafx_base_mac"
    ]
)

In this case, Main.java is actually the only file in my application, as I am trying to just get the program to run. The build seems to work fine with bazel build :app, but when I try to run it with bazel run :app, I get the following error:

Error: JavaFX runtime components are missing, and are required to run this application

Can somebody please help me resolve this?


Solution

  • https://github.com/deepinthink-pumpkin/pumpkin-chat-jfx/blob/master/main/BUILD.bazel#L21

    Create another main class as javafx application entrance.