# How to find resources in Kotlin?

**URL:** <https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315>\
**Category:** Uncategorized\
**Created:** [March 8, 2015, 10:55pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315 "2015-03-08T22:55:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bebarker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bebarker/32/1514_2.png) [@bebarker](https://discuss.kotlinlang.org/u/bebarker)\
**Post date:** [March 8, 2015, 10:55pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/1 "2015-03-08T22:55:13Z")

</div>

I thought I'd find the answer to this, and indeed my code compiles, but I eventually get a runtime error.

Here’s some simple code that tries to create a JavaFX UI from an FXML specification (a resource):

```
package ui

import kotlinfx.builders.\*  
import kotlinfx.properties.\*  
import javafx.application.Application  
import javafx.stage.Stage  
import javafx.fxml.FXMLLoader

 

fun main(args: Array\<String\>) {  
&nbsp;&nbsp;Application.launch(javaClass\<LaRGUI\>())  
}

 

class LaRGUI : Application() {  
&nbsp;&nbsp;override fun start(stage: Stage?) {  
&nbsp;&nbsp;print(javaClass.getResource(“main.fxml”));  
&nbsp;&nbsp;Stage(stage, title = “Log and Report”) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;scene = Scene(width = 900.0, height = 550.0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;root = FXMLLoader.load(**javaClass.getResource(“main.fxml”)**);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;}  
&nbsp;&nbsp;}.show()  
&nbsp;&nbsp;}  
}
```

```

```

Here is the error:  
  
   
$ ~/gradle-2.3/bin/gradle ui  
:compileKotlin  
w: Annotations path entry points to a non-existent location:  
:compileJava UP-TO-DATE  
:processResources  
:classes  
:jar  
:ui  
nullException in Application start method  
Exception in thread “main” java.lang.reflect.InvocationTargetException  
 &nbsp;&nbsp;at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
 &nbsp;&nbsp;at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
 &nbsp;&nbsp;at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
 &nbsp;&nbsp;at java.lang.reflect.Method.invoke(Method.java:483)  
 &nbsp;&nbsp;at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)  
Caused by: java.lang.RuntimeException: Exception in Application start method  
 &nbsp;&nbsp;at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)  
 &nbsp;&nbsp;at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)  
 &nbsp;&nbsp;at com.sun.javafx.application.LauncherImpl$$Lambda$48/99550389.run(Unknown Source)  
 &nbsp;&nbsp;at java.lang.Thread.run(Thread.java:745)  
Caused by: java.lang.NullPointerException: Location is required.  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)  
 &nbsp;&nbsp;at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)  
 &nbsp;&nbsp;at ui.LaRGUI$start$1$1.invoke(main.kt:24)  
 &nbsp;&nbsp;at ui.LaRGUI$start$1$1.invoke(main.kt:19)  
 &nbsp;&nbsp;at kotlinfx.builders.BuildersPackage$Scene$e907cc0b.Scene(Scene.kt:83)  
 &nbsp;&nbsp;at kotlinfx.builders.BuildersPackage.Scene(Unknown Source)  
 &nbsp;&nbsp;at ui.LaRGUI$start$1.invoke(main.kt:23)  
 &nbsp;&nbsp;at ui.LaRGUI$start$1.invoke(main.kt:19)  
 &nbsp;&nbsp;at kotlinfx.builders.BuildersPackage$Stage$651bb459.Stage(Stage.kt:48)  
 &nbsp;&nbsp;at kotlinfx.builders.BuildersPackage.Stage(Unknown Source)  
 &nbsp;&nbsp;at ui.LaRGUI.start(main.kt:22)  
 &nbsp;&nbsp;at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)  
 &nbsp;&nbsp;at com.sun.javafx.application.LauncherImpl$$Lambda$51/1084860161.run(Unknown Source)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl$$Lambda$45/1469821799.run(Unknown Source)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl$$Lambda$47/781040349.run(Unknown Source)  
 &nbsp;&nbsp;at java.security.AccessController.doPrivileged(Native Method)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)  
 &nbsp;&nbsp;at com.sun.javafx.application.PlatformImpl$$Lambda$46/626202354.run(Unknown Source)  
 &nbsp;&nbsp;at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)  
 &nbsp;&nbsp;at com.sun.glass.ui.win.WinApplication.\_runLoop(Native Method)  
 &nbsp;&nbsp;at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)  
 &nbsp;&nbsp;at com.sun.glass.ui.win.WinApplication$$Lambda$37/128893786.run(Unknown Source)  
 &nbsp;&nbsp;… 1 more  
:ui FAILED

 

I’ve attached an image of my project structure. Also, I verfied main.fxml is getting put into the jar file:

$ unzip LaR.jar  
Archive:&nbsp;&nbsp;LaR.jar  
&nbsp;&nbsp;creating: META-INF/  
&nbsp;&nbsp;inflating: META-INF/MANIFEST.MF  
&nbsp;&nbsp;creating: ui/  
&nbsp;&nbsp;inflating: ui/LaRGUI$start$1$1.class  
&nbsp;&nbsp;inflating: ui/LaRGUI$start$1.class  
&nbsp;&nbsp;inflating: ui/LaRGUI.class  
&nbsp;&nbsp;inflating: ui/UiPackage$main$95ba707c.class  
&nbsp;&nbsp;inflating: ui/UiPackage.class  
**&nbsp;&nbsp;inflating: main.fxml**

 

Any ideas what I may be doing wrong? Thanks!

  
  
 [LaR\_Resource.PNG](https://discuss.kotlinlang.org/uploads/short-url/8Sflst7z2nflT2FLQ1HSJtdEuy6.PNG) (75.8 KB)

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 9, 2015, 6:34am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/2 "2015-03-09T06:34:24Z")

</div>

See: [http://stackoverflow.com/a/22011185](http://stackoverflow.com/a/22011185)

---

<div class="post-metadata">

**Author:** ![bebarker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bebarker/32/1514_2.png) [@bebarker](https://discuss.kotlinlang.org/u/bebarker)\
**Post date:** [March 9, 2015, 1:19pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/3 "2015-03-09T13:19:59Z")

</div>

I had actually looked at that page, but didn't see anything very helpful. getClass is apparently replaced by javaClass in Kotlin, which I was able to compile successfuly. I believe I've also put my fxml resource in the designated place.

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 9, 2015, 2:00pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/4 "2015-03-09T14:00:42Z")

</div>

Have you tried "javaClass.getClassLoader().getResource("main.fxml")" or "javaClass.getResource("/main.fxml")"

---

<div class="post-metadata">

**Author:** ![bebarker](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bebarker/32/1514_2.png) [@bebarker](https://discuss.kotlinlang.org/u/bebarker)\
**Post date:** [March 9, 2015, 2:24pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/5 "2015-03-09T14:24:29Z")

</div>

Thanks, it was a bit strange to me that getResource existed at two levels; I suppose the way I was using it was trying to get the resource of the javaClass class...

Also, I had to specify the class like this:

&nbsp;

```kotlin
javaClass<LaRGUI>().getClassLoader().getResource("main.fxml")
```

`
`

`I saw some code floading around like this:`

`
Paths.get(javaClass<Unit>().getClassLoader()!!.getResource(“.”)!!.getPath()!!)!!.
getParent()!!.getParent()!!.getParent()!!.toFile().getAbsolutePath()
Any idea what the double “!” operator is?
`

---

<div class="post-metadata">

**Author:** ![eliteSchaf](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@eliteSchaf](https://discuss.kotlinlang.org/u/eliteSchaf)\
**Post date:** [March 9, 2015, 2:27pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/6 "2015-03-09T14:27:05Z")

</div>

Its explained in the documentation: [http://kotlinlang.org/docs/reference/null-safety.html#the--operator](http://kotlinlang.org/docs/reference/null-safety.html#the--operator)

---

<div class="post-metadata">

**Author:** ![anidotnet](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/anidotnet/32/2880_2.png) [@anidotnet](https://discuss.kotlinlang.org/u/anidotnet)\
**Post date:** [June 23, 2017, 5:53am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/7 "2017-06-23T05:53:50Z")

</div>

This trick does not seems to work now from inside lambdas. Any idea how to get the resources? I am using lambda as part of a dsl.

`javaClass.classLoader.getResource("abcd.xyz")` always returns null from lambda, but it is returning the resources from a normal top-level class.

---

<div class="post-metadata">

**Author:** ![thebali](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/thebali/32/5711_2.png) [@thebali](https://discuss.kotlinlang.org/u/thebali)\
**Post date:** [November 5, 2018, 8:12pm UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/8 "2018-11-05T20:12:41Z")

</div>

Do we have any better solutions to this?  
I am currently using the below, which does not work sometimes,

```
private val soundUri = Uri.parse("android.resource://" + MainActivity::class.java.`package`!!.name + "/" + R.raw.audio_file)

```

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [November 6, 2018, 8:56am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/9 "2018-11-06T08:56:16Z")

</div>

This is not a Kotlin problem, rather an android problem. You’re supposed to access resources (based on a context) using the Resources class. In this case [Resources &nbsp;|&nbsp; Android Developers](https://developer.android.com/reference/android/content/res/Resources.html#openRawResourceFd(int)) or [Resources &nbsp;|&nbsp; Android Developers](https://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int)) might be the solution you’re looking for. Note that neither is accessible from outside the package. Btw. You get the package not from the MainActivity, but you can [inject it into BuildConfig through a gradle configuration](https://developer.android.com/studio/build/gradle-tips#share-custom-fields-and-resource-values-with-your-app-code)

---

<div class="post-metadata">

**Author:** ![thebali](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/thebali/32/5711_2.png) [@thebali](https://discuss.kotlinlang.org/u/thebali)\
**Post date:** [November 6, 2018, 11:37am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/10 "2018-11-06T11:37:01Z")

</div>

The two methods you have linked in the response, return InputStream, I would require a Uri to the resource file.

Thank you for your reply.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [November 6, 2018, 11:41am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/11 "2018-11-06T11:41:44Z")

</div>

You can do that. There is some further info [here](https://stackoverflow.com/questions/45722227/how-to-get-an-uri-from-a-raw-resource). But what is called package has been renamed to applicationId. It is the name of your app as far as android is concerned (the name in the manifest). It is actually available from the [`Context`](https://developer.android.com/reference/android/content/Context#getPackageName())

[This answer](https://stackoverflow.com/a/38340580/4100) for images has a good solution

---

<div class="post-metadata">

**Author:** ![thebali](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/thebali/32/5711_2.png) [@thebali](https://discuss.kotlinlang.org/u/thebali)\
**Post date:** [November 6, 2018, 11:58am UTC](https://discuss.kotlinlang.org/t/how-to-find-resources-in-kotlin/315/12 "2018-11-06T11:58:44Z")

</div>

Actually, my solution for Uri works. I just wanted to have a cleaner way of writing the Uri to a resource file.

Only `MainActivity::class.java.`package`!!.name` portion of the string is a bit weird. I wanted this to be a bit cleaner.

_Edit_: and above mentioned version of string does not work when I enable the debug version (using applicationIdSuffix “.debug”) of the app to be deployed on the device. Some light on the debug thing here.

Thank you for the links. 🙂
