~ read.

Proper way of loading resources in scala

My initial goal for my fabricator library that i'm working on is that it can be used in both Java and Scala projects. The problem i faced recently was that , when i packaged scala code into jar file and tried to use it in another java project, i got "file not found" exception in a step when my library was trying to read data from resources file. My project structure was simple :

  • src
    • main
      • java
      • resources
      • scala

At first, i was trying to read my resource file by giving relative path , but unfortunately this doesn't work when it comes to packaging your code.
Eventually, i found a nice and working way to load resources in scala :
Source.fromInputStream(getClass().getClassLoader().getResourceAsStream("filename")
That give's an InputStream which you can process then however you want.

comments powered by Disqus
comments powered by Disqus