2

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.

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • I don't know anything about Fantom (looks cool from a quick glance though), but it seems there is some Classloader manipulation/isolation going on. Might have something to do with something in this area, for example: http://fantom.org/sidewalk/topic/1208. – Charlie Collins Nov 22 '10 at 17:09
  • Try addressing the resource with its full path starting with / and see if that helps. – Knubo Nov 22 '10 at 18:34
  • What does the "full path" mean? The resource is in a jar file. – Jorn Nov 23 '10 at 15:13
  • So if I undersand this correctly you want to access a .jar file from Fantom and that .jar file contains a svg? Or is it the other way around? – Daniel Fath Mar 17 '11 at 09:13

4 Answers4

2

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`)
afrankvt
  • 181
  • 3
1

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

Java: AnyClassFromJar.class.getClassLoader() works

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

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
Alex Panchenko
  • 452
  • 2
  • 6
0

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

Paul
  • 3,009
  • 16
  • 33
0

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)
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82