Gradle项目中ObservableValue.subscribe()调用示例及问题解决
Gradle项目(含Wrapper)中使用
ObservableValue.subscribe()的问题与解决 问题背景
尝试在Gradle项目中调用JavaFX 21新增的ObservableValue.subscribe()方法时,编译报error: cannot find symbol错误,即使在build.gradle.kts中指定了JavaFX 24版本,仍怀疑Gradle加载了不包含该方法的旧版本。
原代码与配置
build.gradle.kts
repositories { mavenCentral() } plugins { java application id("org.openjfx.javafxplugin") version "0.1.0" } javafx { version = "24" modules("javafx.base") } application { mainClass = "Main" }
src/main/java/Main.java
import java.util.function.Consumer; import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.value.ObservableValue; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { ObservableValue<Object> prop = new SimpleObjectProperty<Object>(null); prop.subscribe(new Consumer<Object>() { public void accept(Object t) {} }); } }
编译错误
执行./gradlew build后报错:
> Task :compileJava FAILED /home/user/question/src/main/java/Main.java:11: error: cannot find symbol prop.subscribe(new Consumer<Object>() { public void accept(Object t) {} }); ^ symbol: method subscribe(<anonymous Consumer<Object>>) location: variable prop of type ObservableValue<Object> 1 error [Incubating] Problems report is available at: file:///home/user/question/build/reports/problems/problems-report.html FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed; see the compiler output below. /home/user/question/src/main/java/Main.java:11: error: cannot find symbol prop.subscribe(new Consumer<Object>() { public void accept(Object t) {} }); ^ symbol: method subscribe(<anonymous Consumer<Object>>) location: variable prop of type ObservableValue<Object> 1 error * Try: > Check your code and dependencies to fix the compilation error(s) > Run with --scan to get full insights. BUILD FAILED in 924ms 1 actionable task: 1 executed
后续测试
使用第三方提供的代码测试后发现:直接用gradle run可成功运行,但使用Gradle Wrapper的./gradlew run仍报错。
非Wrapper运行结果
$ tree -a . ├── build.gradle.kts ├── settings.gradle.kts └── src └── main └── java └── com └── example └── Main.java 6 directories, 3 files $ gradle -version ------------------------------------------------------------ Gradle 8.10.2 ------------------------------------------------------------ Build time: 2024-09-23 21:28:39 UTC Revision: 415adb9e06a516c44b391edff552fd42139443f7 Kotlin: 1.9.24 Groovy: 3.0.22 Ant: Apache Ant(TM) version 1.10.14 compiled on August 16 2023 Launcher JVM: 21.0.5 (Oracle Corporation 21.0.5+1-nixos) Daemon JVM: /nix/store/f042x32jfm94d3cgaga8d6xl8vy6sg46-openjdk-21.0.5+11/lib/openjdk (no JDK specified, using current Java home) OS: Linux 6.6.69 amd64 $ gradle run > Task :run Property value = null Property value = Hello, World! Property value = Hi, mom! BUILD SUCCESSFUL in 529ms 2 actionable tasks: 2 executed
Wrapper运行结果
$ tree -a . ├── build.gradle.kts ├── settings.gradle.kts └── src └── main └── java └── com └── example └── Main.java 6 directories, 3 files $ gradle wrapper BUILD SUCCESSFUL in 449ms 1 actionable task: 1 executed $ ./gradlew -version ------------------------------------------------------------ Gradle 8.10.2 ------------------------------------------------------------ Build time: 2024-09-23 21:28:39 UTC Revision: 415adb9e06a516c44b391edff552fd42139443f7 Kotlin: 1.9.24 Groovy: 3.0.22 Ant: Apache Ant(TM) version 1.10.14 compiled on August 16 2023 Launcher JVM: 21.0.5 (Oracle Corporation 21.0.5+1-nixos) Daemon JVM: /nix/store/vc52hvhclai81s6yyg7mng0g0mfvnqim-openjdk-21.0.5+11/lib/openjdk (no JDK specified, using current Java home) OS: Linux 6.6.69 amd64 $ ./gradlew run > Task :compileJava FAILED /home/user/question/src/main/java/com/example/Main.java:16: error: cannot find symbol property.subscribe(value -> System.out.println("Property value = " + value)); ^ symbol: method subscribe((value)->S[...]alue)) location: variable property of type SimpleObjectProperty<Object> 1 error FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed; see the compiler error output for details. * Try: > Run with --info option to get more log output. > Run with --scan to get full insights. BUILD FAILED in 446ms 1 actionable task: 1 executed
问题解决
最终定位为Java安装配置问题:Gradle和Gradle Wrapper使用了不同的JAVA_HOME和Java安装环境。在NixOS的overlay中原本配置:
p.jdk.override { enableJavaFX = true; openjfx_jdk = p.openjfx.override { withWebKit = true; }; };
将p.openjfx.override改为p.openjfx23.override后问题解决。目前仍存在疑问:为何Gradle Wrapper会优先使用系统Java安装中的JavaFX,而非build.gradle.kts中指定的版本。
内容的提问来源于stack exchange,提问作者Michael D. Adams
相关产品推荐
相关产品推荐

