3

While studying using Scala with JavaFX I have met the following code in a ProScalaFX example:

  val resource = getClass.getResource("AdoptionForm.fxml")
  if (resource == null) {
    throw new IOException("Cannot load resource: AdoptionForm.fxml")
  }

  ...

  val root: jfxs.Parent = jfxf.FXMLLoader.load(resource)

Where do I put the actual "AdoptionForm.fxml" content in this case? Unfortunately I am neither familiar with using resources in Java.

I use SBT as the building system and Idea as an IDE.

There is a related question which suggests a way (putting the resource files in "src/main/resources" or "src/main/resources/packagename"), but it also says it doesn't work actually (needless to say I have tried).

Community
  • 1
  • 1
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • I put my resources in `src/main/resources` using SBT 0.12.x and it works perfectly. Is it possible that your problem is related to the [difference between `getClass.getResource` and `ClassLoader.getResource`](http://stackoverflow.com/q/6608795/1804173) and absolute vs relative locations? – bluenote10 Apr 29 '14 at 10:37
  • Perhaps, @bluenote10. Would you be so kind to provide a working example? – Ivan Apr 29 '14 at 10:39
  • not that easy since it's just all about the package/resource directory structure. You may try this: `getClass.getResource("/TopLevelResource.fxml")` and ensure that the file has the path: `src/main/resources/TopLevelResource.fxml` (prepending the string with a `/` switches from package-base to absolute resource locations). – bluenote10 Apr 29 '14 at 10:46
  • Adding "/" has indeed solved the problem. Thanks, @bluenote10. – Ivan Apr 29 '14 at 10:54
  • I hope you don't mind converting the comment into an answer. – bluenote10 Apr 29 '14 at 11:06

1 Answers1

5

src/main/resources is the correct location for placing resources in a default SBT configuration.

However, one has to be aware of the difference between getClass.getResource and ClassLoader.getResource. Using getClass.getResource("AdoptionForm.fxml") requires the file to be located in a path which corresponds to the package of the class.

For instance: If the class is located in com.domain.utils then the resource must be located at src/main/resources/com/domain/utils/AdoptionForm.fxml.

In order to switch from package-relative locations to absolute locations one can either use ClassLoader.getResource or just prepend the resource string with a /.

Example: getClass.getResource("/AdoptionForm.fxml") loads the resource from src/main/resources/AdoptionForm.fxml

Community
  • 1
  • 1
bluenote10
  • 23,414
  • 14
  • 122
  • 178
  • Putting the resource to the package-corresponding path did't work though. My root package is just like `MyApp` and the only class (an object actually) is `Main` (`extends JFXApp`). I have tried `src/main/resources/MyApp/MainForm.fxml` and `src/main/resources/MyApp/Main/MainForm.fxml` with no luck. – Ivan Apr 29 '14 at 11:21
  • This is strange. Are you running your example in SBT or in IDEA? In the latter case, [this](http://stackoverflow.com/questions/12133632/scala-getclass-getresource-returning-null) + [that](http://stackoverflow.com/questions/11176969/intellij-how-to-make-non-java-files-copied-to-the-bin-directory-as-well) may help? – bluenote10 Apr 29 '14 at 11:56
  • I am trying to use Idea to run this project. Thanks for the links. – Ivan Apr 29 '14 at 12:30