繁体   English   中英

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

[英]Getting The URL of Resource in Another Module

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我的图书馆(图书馆模块)。

综上所述:我需要在客户端模块中获取 FXML 文件的资源 URL 以在库模块中读取。

我试过了:

从外部模块访问资源文件

从 Java 中的另一个模块加载资源(使用 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 控制器 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);
    }
}

我收到以下错误:

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找到

在如何从VGR正确制作 URL 的一些帮助下,我能够使用ClassGraph库解决问题

以下代码解决了所有问题(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;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM