I am writting a library to ease the development of JavaFX application, and in order to make it usable with Java modular system, I need to get the URL of the FXML file in another java module (the client module) and pass it to FXMLLoader
in my library (the library module).
So to sum: I need to get the resource URL of an FXML file in the client module to be read in the library module.
I tried:
Accessing resource files from external modules
Load a Resource from another Module in Java (With Maven)
My latest attempt:
private void addController(ControllerInfo info, Class<? extends SimpleController> controllerClass) throws IOException {
String filename = info.FXMLFile();
if (!filename.startsWith("/"))
filename = "/" + filename;
ModuleReference ref=ModuleLayer.boot().configuration().findModule(moduleName).map(ResolvedModule::reference).get();
ModuleReader resReader=ref.open();
URL url = resReader.find(filename).get().toURL();
controllerClasses.put(info.Id(), controllerClass);
info.Type().getAction().addController(info, url, controllerClass);
}
In the client module I have placed my resources inside a io/github/ossnass/languageexample
folder in the resources folder, and the module-info.java
looks like this:
module languageExample {
requires javafx.controls;
requires javafx.fxml;
requires simplefx;
opens io.github.ossnass.languageexample.gui to javafx.fxml;
opens io.github.ossnass.languageexample to simplefx;
exports io.github.ossnass.languageexample.app;
}
The FXML contoller class:
@ControllerInfo(Id = "LangMain", FXMLFile = "/io/github/ossnass/languageexample/langexample.fxml", Type = ContollerType.SINGLE_INSTANCE_ON_STARTUP)
public class LangExampleMain extends SimpleController {
@Override
protected void userInit() {
}
@Override
protected void onStageShowUser() {
}
@FXML
private Button btnMessage;
@FXML
void btnMessageClick(ActionEvent event) {
QuickActions.showInfoMessage(null,
resources.getString("InfoHeader"),
resources.getString("InfoBody"),
this);
}
}
I get the following error:
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:148)
at io.github.ossnass.fx.ControlMaster.addController(ControlMaster.java:179)
at io.github.ossnass.fx.ControlMaster.findControllers(ControlMaster.java:198)
at io.github.ossnass.fx.ControlMaster.initControlMaster(ControlMaster.java:134)
at io.github.ossnass.languageexample.app.Main.start(Main.java:11)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
The URL path to the resource file is :/io/github/ossnass/languageexample/langexample.fxml
Please notice that I am using Maven and I put the FXMLs in the resources
dir.
The library I am developing can be found at SimpleFX
I was able to solve the problem using ClassGraph
library with some help of how to properly make the URL from VGR
The following bit of code solved all (Java 8 and Java 9+ with modular system):
public static Resource getRescoure(String urlStr) {
try (ScanResult scan = new ClassGraph().scan()) {
if (urlStr.startsWith("/"))
urlStr = urlStr.substring(1);
ResourceList rl = scan.getResourcesWithPath(urlStr);
if (rl.size() > 0)
return rl.get(0);
return null;
}
}