IKVM

编程入门 行业动态 更新时间:2024-10-09 17:24:25
IKVM - getResourceAsStreams返回null(IKVM - getResourceAsStreams returns null)

在将具有IKVM的Java库编译到DLL中之后,我收到以下错误:

getResourceAsStream("path/to/resource.file") == null"

库尝试使用以下代码从自己的jar文件加载Java资源:

Thread.currentThread().getContextClassLoader().getResourceAsStream("path/to/resource.file")

作为解决方法,我尝试将资源文件复制到我的.NET项目的主目录中,并使用resource参数调用ikvmc:

ikvmc java-library.jar -target:library -resouce:/path/to/resource.file=resource.file

奇怪的是,生成的DLL包含一个resource.jar,它只包含一个空目录strukture /path/to/resource.file ,其中resource.file不是文件,而只是另一个空目录

有没有办法用ikvmc将Java库编译成DLL,其中Java代码使用ContextClassLoader.getResourceAsStream()读取资源文件?

after compiling a Java library with IKVM into an DLL, I get the following error:

getResourceAsStream("path/to/resource.file") == null"

the library tries to load a Java resouce from the own jar file with the following code:

Thread.currentThread().getContextClassLoader().getResourceAsStream("path/to/resource.file")

as workaround I tried to copy the resouce file into the main directory of my .NET project and call ikvmc with the resource parameter:

ikvmc java-library.jar -target:library -resouce:/path/to/resource.file=resource.file

the strange thing is that the resulting DLL includes a resource.jar containing just an empty directory strukture /path/to/resource.file where resource.file is not the file, but just another empty directory

is there a way to compile Java libraries with ikvmc into DLLs, where the Java code reads resource files with ContextClassLoader.getResourceAsStream()?

最满意答案

这个问题是由类加载器的解决引起的。 Java库使用上下文类加载器:

Thread.currentThread().getContextClassLoader()

在IKVM中,似乎这是用于整个.NET运行时的通用类​​加载器。 这个无法从JAR文件加载资源。 更改Java库以使用加载包含资源加载代码的类的相同类加载器时,资源也与IKVM正确加载:

ClassLoader currentClassLoader = MyClass.class.getClassLoader(); URL url = currentClassLoader.getResource("resource/name");

The problem was caused by the decission on the class loader. The Java library uses the context class loader:

Thread.currentThread().getContextClassLoader()

In IKVM it seems that this is the general class loader used for the entire .NET runtime. This one is not able to load resources from JAR files. When changing the Java library to use the same class loader that loaded the class containing the resource loading code, the resources are loaded corretly with IKVM as well:

ClassLoader currentClassLoader = MyClass.class.getClassLoader(); URL url = currentClassLoader.getResource("resource/name");

更多推荐

本文发布于:2023-08-04 18:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IKVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!