#1571 Can Fantom access resources from .jar it uses?

DanielFath Sun 3 Jul 2011

Basically does Fantom allow us to use an equivalent to Java's Thread.currentThread().getContextClassLoader().getResourceAsStream("name") or to be more precise ClassLoader().getResourceAsStream("name")?

EDIT: I'm looking for solution to this problem: Stack Overflow: Why Can't Fantom find the resource in a jar

brian Sun 3 Jul 2011

You could use that exact Java code via FFI. Haven't tried it but it should work.

But instead I would recommend just using the standard Pod.file API. It will work in all environments (JVM pods, JS, or JVM jar).

DanielFath Sun 3 Jul 2011

How would pod work? If I understood correctly the problem was that he was trying to access a file which is inside a jar he uses in lib/java. Apparently the jar is visible by the Fantom, but he can't access the .jar inner structure and retrieve a file. The problem isn't finding the correct .jar its accessing its inner resources without the need to unzip the file manually.

alex_panchenko Sun 3 Jul 2011

The question on stackoverflow is quite old :)

getContextClassLoader() depends on how classloaders are created by runtime, so I don't think it's usage is correct here.

In java it should be like this: AnyClassFromJar.class.getClassLoader()

But in fantom I afraid the only way is Class.forName("AnyClassFromJar").getClassLoader()

brian Sun 3 Jul 2011

Alex is correct - using your Thread's context loader is probably never safe. If you want to load a class from a specific jar, then you should use the loader of a class you know to live inside that jar. In Fantom you can get the java.lang.Class a couple of ways:

Class.forName("Foo")
Foo#->toClass
Interop.toFan(cls)
Interop.toJava(type)

See Java FFI.

Login or Signup to reply.