Location is not set in multimodule JavaFx gradle project
I am converting a monolithic project into a multi module project, everything worked fine before but now I get the error java.lang.lllegalStateException: location is not set. I can't figure out what's the problem
I have three modules : core, fx_view and service, plus the root module where my main class is.
both the root module and fx_view use Javafx.
The fxml is in the resources folder in the fx_view module and I want to reference it from the root module. MainController is in the fx_view module as well.
Here's the result I get from the following print statements
URL url = ModuleLayer.boot().findModule("com.spimelabs.medicalis.fx_view").orElseThrow().getClassLoader().getResource("/com/spimelabs/medicalis/fx_view/main_view.fxml");
System.out.println("Location is =" + url);
System.out.println("resources root is =" + MainController.class.getClassLoader().getResource(""));
System.out.println("location 2 = " + MainController.class.getClassLoader().getResource("com/spimelabs/medicalis/fx_view/main_view.fxml"));
System.out.println("ClassLoader =" + ModuleLayer.boot().findModule("com.spimelabs.medicalis.fx_view").orElseThrow().getClassLoader());
System.out.println("Module =" + MainController.class.getModule().getName());
Location is =null
resources root is =null
location 2 = null
ClassLoader =jdk.internal.loader.ClassLoaders$AppClassLoader@7ba4f24f
Module =com.spimelabs.medicalis.fx_view
root build
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
}
group = 'com.spimelabs'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
application {
mainClass = 'com.spimelabs.medicalis.bootstrap.MainApp'
mainModule = 'medicalis'
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':com.spimelabs.medicalis.core')
implementation project(':com.spimelabs.medicalis.fx_view')
implementation project(':com.spimelabs.medicalis.service')
}
javafx {
version = "23.0.1"
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics']
}
test {
enabled = false
useJUnitPlatform()
}
fx_view build
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.1.0'
}
group = 'com.spimelabs'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':com.spimelabs.medicalis.core')
}
javafx {
version = "23.0.1"
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.graphics']
}
java {
modularity.inferModulePath = true
}
test {
useJUnitPlatform()
}
settings.gradle
rootProject.name = 'medicalis'
include ':com.spimelabs.medicalis.core', ':com.spimelabs.medicalis.fx_view', 'com.spimelabs.medicalis.service'
It seems that there's a problem in the gradle configuration since
System.out.println("resources root is =" + MainController.class.getClassLoader().getResource(""));
is returning null. What do you think?
module-info.java of the root module
module medicalis {
requires com.spimelabs.medicalis.fx_view;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
opens com.spimelabs.medicalis.bootstrap to javafx.graphics, javafx.fxml;
}
module-info.java of fx_view
module com.spimelabs.medicalis.fx_view {
requires com.spimelabs.medicalis.core;
requires java.prefs;
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
opens com.spimelabs.medicalis.fx_view to javafx.fxml, javafx.graphics;
opens com.spimelabs.medicalis.fx_view.controller to javafx.fxml;
exports com.spimelabs.medicalis.fx_view;
exports com.spimelabs.medicalis.fx_view.controller;
}Here's the stack trace :
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1164)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:1575)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml@23.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2561)
at javafx.fxml@23.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2536)
at medicalis/com.spimelabs.medicalis.bootstrap.MainApp.start(MainApp.java:29)
at javafx.graphics@23.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@23.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@23.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@23.0.1/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics@23.0.1/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$10(GtkApplication.java:264)
... 1 more
Exception running application com.spimelabs.medicalis.bootstrap.MainApp
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run' (registered by plugin 'org.gradle.application').
> Process 'command '/home/spimees/.sdkman/candidates/java/23-open/bin/java'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
BUILD FAILED in 6s
11 actionable tasks: 1 executed, 10 up-to-date
08:21:30: Execution finished 'run'.
请先登录再写评论。
Hi Adibablu998 , it should be caused by that the “classloader lookup” is blocked when the project is inverted into the multi-module project.
In the current situation, we can try to use
Class.getResource(), which resolves through the calling class's module, so it can read resources that are co-located with that class without needing unconditional opens.Changes to be made from the root module:
Please kindly let us know how it goes after switching to
Class.getResource(), thank you.