Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala getClass.getResource() returning null

I have this code:

val url: URL = getClass.getResource("com/mysite/main/test.fxml") 

and it always returns null (or Unit). I have only two files in the project:

MyProj/src/com/mysite/main/Test.scala MyProj/src/com/mysite/main/test.fxml 

and when I run the Test.scala the url value is always null.

I just tried rebuild the project, I am using IntelliJ IDEA. What am I doing wrong here?

like image 633
Tower Avatar asked Aug 26 '12 20:08

Tower


1 Answers

You have three options:

  • take advantage of relative path to your current package (where Test.class is):

    getClass.getResource("test.fxml") 
  • you can use absolute path:

    getClass.getResource("/com/mysite/main/test.fxml") 
  • or load through the ClassLoader (note that it always start from root):

    getClass.getClassLoader.getResource("com/mysite/main/test.fxml") 

In IntelliJ IDEA, make sure you have added ;?*.fxml to the:

Settings (Preferences on Mac) | Compiler | Resource Patterns.

like image 89
Tomasz Nurkiewicz Avatar answered Sep 25 '22 17:09

Tomasz Nurkiewicz