在将具有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.filethe 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");更多推荐
发布评论