为什么 Fantom 找不到 jar 中的资源?

发布于 2024-10-03 23:31:20 字数 315 浏览 2 评论 0原文

我最近开始使用 Fantom。我有一个包含资源(在本例中为 svg 图像)的 jar 文件。我可以很好地使用 jar 中的类,但资源不会加载: Thread.currentThread().getContextClassLoader().getResourceAsStream("name") returns null。当我在纯 Java 上下文中使用这个完全相同的 jar 时,它可以很好地找到资源。

任何有关如何调试甚至解决此问题的建议将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

司马昭之心 2024-10-10 23:31:20

它必须在罐子里吗?如果将其直接插入 Pod 中,则可以使用访问文件,如下所示:

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

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`)
软甜啾 2024-10-10 23:31:20

错误的部分是 Thread.currentThread().getContextClassLoader()

Java: AnyClassFromJar.class.getClassLoader() 有效

Fantom: Class.forName("AnyClassFromJar") .getClassLoader() 应该可以工作

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

Java: AnyClassFromJar.class.getClassLoader() works

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

神经大条 2024-10-10 23:31:20

如果我没记错的话,.jar 文件无法访问资源,但由于 .jar 文件只不过是具有精美扩展名的 Zip 文件,您可以像这样打开它们。

这是我的示例文件夹结构(但任何结构都可以)。

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

然后您像这样打开 testOpen.jar

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

编辑:在 Fantom 板上对此进行了讨论,看来这个示例应该可行。

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

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)
花落人断肠 2024-10-10 23:31:20

尝试使用“/name”而不是“name”。这对我有用(用Java)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文