Accessing java resources via getResourceAsStream

Is it currently possible to access resources as stored under src/main/resources via Class.getResourceAsStream?

Using resources.enableResourcesFromSourceDirs = true in build.gradle will cause it to package resouces from src/main/java, which seems to be the only way to get java resources to work. This is pretty counterintuitive behavior, since resources/ only seems to contain files used by xcode which aren’t packaged normally into the jar.

There are a couple issues here:

In MOE there are 2 types of resources:

  1. resources in the application.jar - this are created from the jar file dependendencies, and optionally from the source paths (as you wrote above). These resources are only accessible to Java
  2. resources in the iOS app (that were copied there using the Xcode build). These are accessible from Java code and from native code. These are not automatically added from src/main/resources, one has to add them manually to the Xcode project, so the resource copy build phase copies them to the built app.

Our intention was that Class.getResourceAsStream() should access the iOS app resources as well, so we added the iOS app folder to the classpath, and it works fine if the system class loader (ClassLoader.getSystemClassLoader()) or a child of it (like e.g. the Thread.currentThread().getContextClassLoader()) is used. However, in MOE all classes are loaded from a single image on the boot class path. So when you are trying to load an iOS resource with X.class.getResourceAsStream(), it won’t work in MOE 1.2.

We will be working on resolving these issues for MOE 1.3.

I created a GitHub issue for tracking.

1 Like

Thank you very much for the detailed response and explaination. Like you mentioned, adding it to the xcode project works fine, so I can use that setup.

great! if resources can access thru jar file, that will be awesome!

Thanks again

Was this solved? How to read something inside application.jar?