# How to load resources in new MPP

**URL:** <https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251>\
**Category:** Uncategorized\
**Created:** [January 21, 2019, 7:25am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251 "2019-01-21T07:25:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![werrenmi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/werrenmi/32/5230_2.png) [@werrenmi](https://discuss.kotlinlang.org/u/werrenmi)\
**Post date:** [January 21, 2019, 7:25am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/1 "2019-01-21T07:25:44Z")

</div>

Hello

I try to load resources in a new MPP like SampleMain::class.java.getResource(“/sample/config.properties”) without success. When i looking in the build folder, the resources are placed in the build/processedResources folder. But when i start an application with an IDEA run configuration this folder will not get applied to the classpath / classloader. The only workaround i see at the moment is to copy those resources with an custom Gradle copy taks to copy them into the build/classes/kotlin/jvm/main folder and execute this task before main start. Or do i anything wrong?

Regards

---

<div class="post-metadata">

**Author:** ![boon.tek](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/boon.tek/32/2273_2.png) [@boon.tek](https://discuss.kotlinlang.org/u/boon.tek)\
**Post date:** [March 11, 2019, 4:52am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/2 "2019-03-11T04:52:16Z")

</div>

Having the same issue here.

---

<div class="post-metadata">

**Author:** ![pkirill\_j](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@pkirill\_j](https://discuss.kotlinlang.org/u/pkirill_j)\
**Post date:** [March 13, 2019, 4:47pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/3 "2019-03-13T16:47:46Z")

</div>

If MPP is “multiplatform projects prototype” then it suppose to support JS target, right ?  
In JS it is hard and invalid to perform synchronous blocking resource read, since all data fetch in JS is async.

Or am I miss something ?

---

<div class="post-metadata">

**Author:** ![sksamuel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sksamuel/32/1904_2.png) [@sksamuel](https://discuss.kotlinlang.org/u/sksamuel)\
**Post date:** [March 26, 2019, 3:08pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/4 "2019-03-26T15:08:32Z")

</div>

Same issue as well.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 26, 2019, 4:38pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/5 "2019-03-26T16:38:27Z")

</div>

Well, it does not have to be blocking. MPP probably needs some kind of internal way to load resources. For example using coroutines. It does not have to be done on a language level and could be implemented by libraries, but it requires some kind of support from the build system, meaning that resources must be loaded into the webpack bundle on JS.

---

<div class="post-metadata">

**Author:** ![pkirill\_j](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@pkirill\_j](https://discuss.kotlinlang.org/u/pkirill_j)\
**Post date:** [March 27, 2019, 9:45am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/6 "2019-03-27T09:45:04Z")

</div>

> [@werrenmi](#):
>
> I try to load resources in a new MPP like SampleMain::class.java.getResource(“/sample/config.properties”)

> [@darksnake](#):
>
> Well, it does not have to be blocking.

Well, the thread starter initially requested it to be blocking.  
If async is OK, then it should fit in classical multiplatform paradigm: mpp interface and platform implementations: for web, for jvm/android, and for iOS.

So the thread starter can

- declare multiplatform (async) resource loading interface
- implement it on required platforms.
- package the resources in platform-specific way.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 27, 2019, 10:17am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/7 "2019-03-27T10:17:59Z")

</div>

The problem as I see it is not in code, but in packaging and tooling. In JVM the content of `resource` directory is automatically included in jar. On JS, the resource should be somehow added to webpack build. On native, resources should probably come in a separate directory. Then there could be a platform specific resource getter that could load it from default directory for this platform. It should not be very hard to implement, but requires modifications to the gradle mpp plugin.

---

<div class="post-metadata">

**Author:** ![pkirill\_j](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@pkirill\_j](https://discuss.kotlinlang.org/u/pkirill_j)\
**Post date:** [March 27, 2019, 10:44am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/8 "2019-03-27T10:44:36Z")

</div>

Well, if the problem is in the tool-chain and plugin, then it might be possible to file an YT issue describing the problem ?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [March 27, 2019, 10:48am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/9 "2019-03-27T10:48:59Z")

</div>

Like this one: [https://youtrack.jetbrains.com/issue/KT-29311](https://youtrack.jetbrains.com/issue/KT-29311)? I have not found a universal proposal for common way to load resources. Probably I should write it later.

---

<div class="post-metadata">

**Author:** ![msink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/msink/32/3336_2.png) [@msink](https://discuss.kotlinlang.org/u/msink)\
**Post date:** [March 27, 2019, 11:41am UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/10 "2019-03-27T11:41:14Z")

</div>

I hope [GitHub - orangy/kotlinx-files](https://github.com/orangy/kotlinx-files) will support cross-platform read-only `Resources` filesystem.

---

<div class="post-metadata">

**Author:** ![werrenmi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/werrenmi/32/5230_2.png) [@werrenmi](https://discuss.kotlinlang.org/u/werrenmi)\
**Post date:** [April 11, 2019, 12:09pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/11 "2019-04-11T12:09:14Z")

</div>

Sorry for my very late answer!

I think, my description of the problem was not clear enough when i read it now.

I think it’s neither related to JS / shared code or async / sync. It looks to me more like an IDE / classpath / build problem. I attached a very simple MPP project with just a properties file and a main class. If i execute the main function via IDEA run configuration, the resource will not be found.

[mpp.zip](https://discuss.kotlinlang.org/uploads/short-url/sruL7dl8ulCpw0EuWXfAK4xgvHB.zip) (78.2 KB)

---

<div class="post-metadata">

**Author:** ![rnikander](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnikander/32/6881_2.png) [@rnikander](https://discuss.kotlinlang.org/u/rnikander)\
**Post date:** [May 3, 2019, 8:42pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/12 "2019-05-03T20:42:24Z")

</div>

I’m having this same problem as described in the original post, in a project that mixes JVM and JS. People are talking about cross platform resources, but I don’t think it’s that complicated. In JVM code you need to load a resource, but they’re not on the classpath. For example: I’m getting errors from Apache log4j2 because it’s config file is not on the classpath, even though I have it under `src/jvmMain/resources`. It’s under `jvmMain`, not `commonMain` – I only need it on the JVM side.

---

<div class="post-metadata">

**Author:** ![gz\_k](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gz_k/32/1671_2.png) [@gz\_k](https://discuss.kotlinlang.org/u/gz_k)\
**Post date:** [May 9, 2019, 2:24pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/13 "2019-05-09T14:24:19Z")

</div>

> [@rnikander](#):
>
> I don’t think it’s that complicated. In JVM code you need to load a resource, but they’re not on the classpath. For example: I’m getting errors from Apache log4j2 because it’s config file is not on the classp

I agree with you. I think it’s only this bug [https://youtrack.jetbrains.com/issue/KT-24463](https://youtrack.jetbrains.com/issue/KT-24463)

But it should be qualified as BUG and not problem, with a critical priority. Let me explain why.

When you create a multiplatform JS / JVM project using Intellij Idea

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/f/fedc535a29c87a2e604798050968268ac26e1feb.png)

you get a Ktor project with a `jvmMain/resources/logback.xml` file.

![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/b/b14984ddee7de8ae8cad548a1e1e74de0abe4939.png)

Everything looks fine, and you can run the project directly from the `main` function.

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/e/e2a718cefbe7ce3dc92fefd1b9b842622b637813.png)

The problem is that the file is not in the classpath.

It takes time to understand the problem and this is a terrible first experience for someone who is trying to run a multiplatform project.

This bug is specific to the new MPP plugin. The same project split into modules with the “old” multiplatform plugin does not have the problem. However, the old plugin is not mentioned anymore in Kotlin documentation. Maybe it should remain until all the major and critical issues of the new MPP plugin are resolved.

---

<div class="post-metadata">

**Author:** ![werrenmi](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/werrenmi/32/5230_2.png) [@werrenmi](https://discuss.kotlinlang.org/u/werrenmi)\
**Post date:** [May 14, 2019, 6:52pm UTC](https://discuss.kotlinlang.org/t/how-to-load-resources-in-new-mpp/11251/14 "2019-05-14T18:52:55Z")

</div>

Thanks for the explanation, exactly what I had observed too, but i didn’t found that issue.  
I think to use the new MPP is anyway a good choice, at least for growing projects to avoid migrations.

A “workaround” is maybe to use the Gradle application plugin, works perfectly for me.
