0

I am facing an issue where getResourceAsStream is returning Null when trying to read a conf file due to which I am unable to read the conf file. I have gone through few details but still nothing has worked so far:

Below is the project structure. I have added the metastore.conf in the resource folder of the source root

I am using the below code:

val fileStream = getClass.getResourceAsStream("metastore.conf")
val configLines = Source.fromInputStream(fileStream).getLines

enter image description here

Image for my project structure:

enter image description here

enter image description here

RoxaneFelton
  • 129
  • 7
  • Please also provide your code where your trying to read the resource file. – AminMal Jul 29 '22 at 14:24
  • 1
    That is not a valid resource location AFAIK – Luis Miguel Mejía Suárez Jul 29 '22 at 16:44
  • @AminMal I have added the code i am trying to execute – RoxaneFelton Jul 29 '22 at 18:16
  • @LuisMiguelMejíaSuárez: Can you please help me understand where i can access it. I have also kept it the source root directory – RoxaneFelton Jul 29 '22 at 18:17
  • As also @LuisMiguelMejíaSuárez mentioned, it's because of invalid file location. `getResourceAsStream` expects a location from your projects root directory, so this will fix it: `val fileStream = getClass.getResourceAsStream("src/main/resources/metastore.conf")` – AminMal Jul 29 '22 at 18:33
  • @LuisMiguelMejíaSuárez I am still getting Configlines as Null. I am further adding the details of my project structure image – RoxaneFelton Jul 29 '22 at 18:49
  • @RoxaneFelton the solution should be `src/main/resources/metastore.conf` so you can access it the way you want. Not sure why you added that `techm` folder there. - BTw, your code is also in a non-standard location. – Luis Miguel Mejía Suárez Jul 29 '22 at 19:27
  • You need to use `getClass.getResourceAsStream("/metastore.conf")`, otherwise it tries to locate the file relative to the package of whatever `getClass` is. – Mark Rotteveel Jul 30 '22 at 07:15
  • 1
    @LuisMiguelMejíaSuárez Including `src/main/resources` in a resource path is wrong. That path only exists in the project source, not in the runtime artifact. – Mark Rotteveel Jul 30 '22 at 07:16
  • @MarkRotteveel I didn't say that was the path to locate it, I said that was the path to store it so you could get it using `getClass.getResourceAsStream("/metastore.conf")` – Luis Miguel Mejía Suárez Jul 30 '22 at 13:27

0 Answers0