reading file from a subdirectory in resources directory

2,158 views
Skip to first unread message

Thefourtheye dIVi

unread,
Jan 7, 2016, 11:32:14 AM1/7/16
to scala-user
Inside my src/main/resources directory, I have a folder structure like this

config
|
└─codes.json
|
└─codes
  |
  └─com.foo.bar.MyClassName$.json

Now, in my code, I have to read both the json files. So, I am doing something like this

getClass.getResourceAsStream("/config/codes.json")

and this works fine. But when I do something like this

getClass.getResourceAsStream("/config/codes/com.foo.bar.MyClassName$.json")

it is returning null. How can I read com.foo.bar.MyClassName$.json file?

Oliver Ruebenacker

unread,
Jan 7, 2016, 11:45:29 AM1/7/16
to Thefourtheye dIVi, scala-user

     Hello,

  Normally, an app does not see what is in src, but your build system compiles and copies files to elsewhere (for example, SBT ouputs to target folder) according to some rules (e.g. compile *.scala, *.java, copy certain other files). Check where the output files go and check whether the file is there. If not, change the rules.

     Best, Oliver


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

Sakthipriyan Vairamani

unread,
Jan 7, 2016, 8:39:19 PM1/7/16
to Oliver Ruebenacker, scala-user
Okay. I think I am understanding the problem a little bit. My application copies the resources files to ROOT/appresources/ directory, where I can see both the files in the same structure I mentioned before. But to find from where the config directory is getting loaded, I wrote something like this

println(s"[FILE IS ] ${getClass.getResource("/config").getFile}")

and the result I got was something like

file:<user home>/.ivy2/cache/<package name>/<module name>_2.11/jars/<module name>-<version>-SNAPSHOT.jar!/config

It means that, it is trying to read from the jar file only. But the com.foo.bar.MyClassName$.json files doesn't come from that jar and the class in which I am trying to read the com.foo.bar.MyClassName$.json is in the jar shown above only.

So, my question boils down to How I can read the file from the ROOT/appresources/ directory, when the actual class which reads reside in a jar file?

Reply all
Reply to author
Forward
0 new messages