0

Though topic is asked several times. But none of those solutions worked for me.

My project is as follows

../src/main/
    |
    |-- java/media/uqab/api/
    |   |
    |   |-- Api.kt
    |
    |
    |-- assets/
        |
        |-- info_list.json
        

I tried to get the info_list.json file from Api.kt. But getting a null value.

class Api {
    companion object {
        fun getInfoList(): List<Info> {
            //...
            val str: InputStream? = Api::class.java.getResourceAsStream("/assets/info_list.json")
            //...
        }
    }
}

I don't know what happened! It was working even a few days ago.

I also tried getResourceAsStream returns null. But didn't work for me.

Moinul Robin
  • 36
  • 1
  • 10
  • 2
    Did you verify if "assets" folder is in your classpath? – Digsb Nov 01 '21 at 05:48
  • Update the project once and check. – Rose Nov 01 '21 at 06:26
  • This is the answer: https://stackoverflow.com/a/26968924/139985. You can't use the classloader to read assets. They are not on the classpath. You need to use `Context.getAssets()...` to retrieve them. – Stephen C Nov 01 '21 at 12:45
  • @Stephen Yes. I could do it with Context. But I want to do without context. – Dont DownVote Please Nov 01 '21 at 12:59
  • And the two answers I linked to say that you can't. – Stephen C Nov 01 '21 at 13:00
  • @StephenC Previously It was working fine, without any Context. So why do I need it now. I'm not doing any test. So how could it be an answer? – Dont DownVote Please Nov 01 '21 at 13:02
  • See the second Q&A. This is not specific to testing. – Stephen C Nov 01 '21 at 13:02
  • I think I would prefer to trust two answers who say that it can't be done to someone who says it worked before but doesn't work now. I'm not doubting that you believe that it worked before. Just that it did work before. For instance, you might have had an copy of that file in your project's "src/main/res" or "src/main/resources" directory. – Stephen C Nov 01 '21 at 13:16
  • Anyhow, if you do find anything that says that you can load an asset (src/main/asset) via a classloader, people would be interested to know exactly how it is done. – Stephen C Nov 01 '21 at 13:23
  • Though it is not working from inside the library module, in app module is working perfectly. **Why This Is Working In App while Not in Library** – Dont DownVote Please Nov 01 '21 at 15:58

0 Answers0