29

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?

Tower
  • 98,741
  • 129
  • 357
  • 507
  • 1
    possible duplicate of [IntelliJ: how to make non java files copied to the bin directory as well?](http://stackoverflow.com/questions/11176969/intellij-how-to-make-non-java-files-copied-to-the-bin-directory-as-well) – CrazyCoder Aug 26 '12 at 20:47

4 Answers4

41

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.

Tower
  • 98,741
  • 129
  • 357
  • 507
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 4
    Nope, the same thing, it returns null. I've googled and tried those, but none of them work either. – Tower Aug 26 '12 at 20:29
  • 1
    @rFactor Try refreshing the project. – FThompson Aug 26 '12 at 20:31
  • @rFactor: so it must be something with your IDE. All of the above work, at least when I am using IntelliJ and maven project structure. Also are you sure `Test.scala` has correct package declaration? The directory is not enough. – Tomasz Nurkiewicz Aug 26 '12 at 20:33
  • @TomaszNurkiewicz yes the file begins with `package com.mysite.main` and everything runs fine, except that it doesn't find the file and yes the file does exist. What is "refresh"? I pressed "Synchronize" and also "Rebuild project". – Tower Aug 26 '12 at 20:36
  • The file is not copied to the "out" folder structure, although the files "Test$.class" and "Test.class" are. Is that a problem? – Tower Aug 26 '12 at 20:37
  • 1
    http://stackoverflow.com/questions/11176969/intellij-how-to-make-non-java-files-copied-to-the-bin-directory-as-well – CrazyCoder Aug 26 '12 at 20:47
5

Possibly it's not being copied to the bin/ directory from the src/ directory? This happens on recompilation, but if you drop it into the src/ directory after the program is already compiled, the IDE won't know.

ConorR
  • 477
  • 5
  • 9
  • this solved my problem. I just had to to sbt clean compile and then it could see the resources. Thanks for preventing me from wasting any more time on this! – grasshopper Feb 05 '15 at 14:10
2

Late answer but I just had this same problem. The root cause was an incorrect rootProject.name entry in my settings.gradle. Once I fixed that, cleaned, and rebuilt my resource were able to load using getClass().getResource(). Hopefully that helps somebody.

cory.todd
  • 516
  • 5
  • 14
0

If it does not work, you can try with ClassLoader:

ClassLoader.getSystemResource("filename").getPath

The filename should be in the same directory layer

Newt
  • 787
  • 8
  • 15