17

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.

The code is the following

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

Where the desired FXML file is:

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

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

Thank you!

3
  • 1
    Where is the plugin folder located? Commented Jan 17, 2013 at 23:20
  • Your file tree shows a directory called plugin, but your code sample refers to a directory called plugins. Could that be the problem? Commented Jan 18, 2013 at 1:01
  • (getClass().getResource("../plugin/PluginSelection.fxml")); works for me Commented Nov 9, 2015 at 13:20

2 Answers 2

17

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") 
Sign up to request clarification or add additional context in comments.

2 Comments

changed it to: root = FXMLLoader.load(getClass().getResource("/pdpro/gui/dialogues/plugin/PluginSelection.fxml")); and it works like a charm. Thanks!
If my resource was four directories higher than the class (../../../../resource), would I just type ////resource?
2

It seems to me that if we use .getResource only when searching in marked as resource folder. Otherwise, even if folder path is correct, but it's not marked as resource folder we'll got an error. So, I do this way:

FileInputStream fileInputStream = new FileInputStream(new File("src/main/java/CRUD/bnkseekCRUD.fxml")); Parent root = loader.load(fileInputStream); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.