如何使用Java getResource()从父目录获取资源? [英] How do I use Java getResource() to get a resource from a parent directory?

查看:270
本文介绍了如何使用Java getResource()从父目录获取资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经尝试了许多方法,包括来自Commons IO的FilenameUtils.normalize(),但是我似乎无法在另一个文件夹中获取资源来获取Java FXML文件.

I've tried numerous methods now, including FilenameUtils.normalize() from commons IO, but I can't seem to be able to get a resource in another folder to get a Java FXML file.

代码如下

  try {
     root = FXMLLoader.load(getClass().getResource("../plugin/PluginSelection.fxml"));
  } catch (IOException ex) {
     Logger.getLogger(QueueOperationsController.class.getName()).log(Level.SEVERE, null, ex);
  }

所需的FXML文件在哪里:

Where the desired FXML file is:

gui
   dialogues
      plugins
         PluginSelection.fxml // desired file
      dataset
         QueueOperationsController // current class

如何最好地获取所需文件的URL?

How do I best get the desired file's URL?

谢谢!

推荐答案

您可以获得相对于Class或上下文根的资源.在示例中,如果这是应用程序中的包结构,则将/放在字符串的开头.试试

You can get resources relative to the Class or the context root. In your example putting / at the start of the string if thats your package structure in your application. Try

getClass().getResource("/gui/dialogues/plugins/PluginSelection.fxml")

这篇关于如何使用Java getResource()从父目录获取资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆