Question

I've recently gotten started using Fantom. I've got a jar file that contains a resource (svg image, in this case). I can use the classes from the jar just fine, but the resource won't load: Thread.currentThread().getContextClassLoader().getResourceAsStream("name") returns null. When I use this exact same jar in a Java-only context, it can find the resource just fine.

Any suggestions on how to debug or even solve this issue would be much appreciated.

Was it helpful?

Solution

The wrong part is Thread.currentThread().getContextClassLoader()

Java: AnyClassFromJar.class.getClassLoader() works

Fantom: Class.forName("AnyClassFromJar").getClassLoader() should work

OTHER TIPS

Does it have to be in a Jar? If you stick it directly in your Pod, you can use access files like this:

file := Pod.find("myPod").file(`/path/to/file.ext`)

If I recall correctly, .jar files can't be accessed for resoruces but since .jar files are nothing more than Zip files with fancy extension you can open them like that.

Here is my folder structure for the example (but any structure will do).

ExampleDir
 |
 +--- TestZip.fan
 +--- testOpen.jar
       |
       +--- META-INF
       |     +--- email.png
       |     ...
       |...

And you open the testOpen.jar like this:

class TestZip
{
  static Void main(Str[] args)
  {
    jar := Zip.open(File(`testOpen.jar`))
    png := jar.contents[`/META-INF/email.png`].readAllBuf
    jar.close
  }
}

EDIT: Having a discussion about this on Fantom boards it seems that this example should work.

buff := Interop.toFan(Class.forName("net.testOpen.Foo").getClassLoader().getResourceAsStream("email.png")),4096)

Try '/name' instead of 'name'. That works for me (in Java).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top