在另一个模块中获取资源的URL

时间:2020-06-06 06:20:34

标签: java maven java-platform-module-system javafx-11

我正在编写一个库来简化JavaFX应用程序的开发,并且为了使其可与Java模块化系统一起使用,我需要在另一个Java模块(客户端模块)中获取FXML文件的URL并将其传递到我的图书馆(图书馆模块)中的FXMLLoader

总而言之: 我需要在客户端模块中获取FXML文件的资源URL,以便在库模块中读取。

我尝试过:

Accessing resource files from external modules

Load a Resource from another Module in Java (With Maven)

我最近的尝试:

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);
    }

在客户端模块中,我将资源放置在资源文件夹的io/github/ossnass/languageexample文件夹中,module-info.java如下所示:

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;
}

FXML控制器类:

@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);
    }
}

我收到以下错误:

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

资源文件的URL路径为:/io/github/ossnass/languageexample/langexample.fxml

请注意,我正在使用Maven,并将FXML放在resources目录中。

我正在开发的库可以在SimpleFX

中找到

1 个答案:

答案 0 :(得分:0)

我可以使用ClassGraph库解决问题,并获得有关如何正确制作VGR的URL的帮助

以下代码可以解决所有问题(带有模块化系统的Java 8和Java 9 +):

    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;
        }
    }