是否可以在Java中加载相同DLL的不同版本? [英] Is it possible to load different versions of the same DLL in Java?

查看:124
本文介绍了是否可以在Java中加载相同DLL的不同版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与一组第三方库交互的JNI库,系统上可能有多个版本的第三方库。对于第三方库的每个版本,出于可比性原因,我必须重新编译JNI代码。现在我通过加载具有特定名称的DLL来处理这个问题,如果版本更改,我更改了JNI接口DLL的名称,以便正确的版本具有正确的名称来加载。

I have a JNI library that interacts with a set of third party libraries, and there may be more than one version of the third party library on the system. For each version of the third party library, I have to re-compile the JNI code for comparability reasons. Right now I deal with this by loading a DLL with a specific name, and if the version changes I change the names of the JNI interface DLLs so that the correct one for the version has the right name to get loaded.

我希望能够根据用户想要使用的版本动态加载dll。如果我在具有不同名称但具有相同方法签名的DLL上调用System.loadLibrary两次会发生什么?

I'd like to be able to dynamically load the dll though, based on which version the user wants to use. What happens if I call System.loadLibrary twice on DLLs with different names but the same method signatures?

System.loadLibrary("JNIv1");
// Same code compiled against a different third party version
System.loadLibrary("JNIv2");

我一次只需要使用其中一个版本,所以如果旧版本是不再可访问。

I only need to use one of the versions at a time, so it's fine if the old version is no longer accessable.

是否可以使用相同的方法签名加载两个不同版本的DLL而无需重新启动程序?

推荐答案

这是可能的,事实上它是完全支持并且运作得非常好。

It is possible and in fact it is totally supported and works brilliantly.

我必须在生产环境和sun JVM上执行此操作,它坚如磐石。

I have had to do this in a production environment and on a sun JVM it is rock solid.

基本上,如果从其他类加载器加载库,然后它将加载库的不同副本。它就这么简单。

Basically, if you load the library from a different classloader, then it will load a different copy of the library. Its as simple as that.

我不建议这样做,除非你真的必须......但确实有效。

I wouldn't recommend doing this unless you really have to... but it does work.

作为替代方案,根据您的具体要求,您可以将其从流程中删除,并在客户端和不同服务器之间使用简单的协议(使用say jetty / xstream / httpclient或netty),每个都加载了不同的dll版本。

As an alternative, depending on your specific requirements, you could just make it out of process, and have a simple protocol (using say jetty/xstream/httpclient, or netty) between the client and the different servers, each of which has a different dll version loaded.

基本上这涉及你编写一个类加载器

Essentially this involves you writing a classloader

public class MyClassLoader extends URLClassLoader {

   protected String findLibrary(String libName) {
         if ( libName.equals("mylib.dll")) {
              return "full/path/to/library";
         }
         else {
              super.findLibrary(libName);
         }
   }
}

然后你安排加载使用相关的类加载器实现您的类......

Then you arrange to load the implementation of your class using the relevant classloader...

public interface Implementation {

}

public class ImplementationLookerUpper {

    Classloader v1 = new MyClassloader(version1);
    Classloader v2 = new MyClassloader(version2);


    public Implementation implementationUsingVersion(Version someversion) {

             Classloader classloader = pickCorrectClassLoaderForVersion(someVersion);

            return (Implementation) classloader.loadClass(RealImplementation.class.getName()).newInstance();

    }
}

那种事...... ..

That kind of thing.....

这篇关于是否可以在Java中加载相同DLL的不同版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆