Java可以尽可能地找到负载库

发布于 2025-02-09 00:06:10 字数 2952 浏览 2 评论 0原文

我正在编写一个Java Web应用程序,该应用程序需要通过Java源代码加载本机C ++ DLL。我已将DLL放置在同一目录处的DLL和所有因DLL。 我的函数addLibraryPath确保添加DLL位置的路径以添加到Java库路径。

我期望这可以作为主DLL和依赖的DLL都位于同一DIR中。

public class Test
{
  public static void addLibraryPath(String pathToAdd) throws Exception
  {
    final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    //get array of paths
    final String[] paths = (String[])usrPathsField.get(null);

    //check if the path to add is already present
    for(String path : paths) {
      if(path.equals(pathToAdd)) {
        return;
      }
    }

    //add the new path
    final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length-1] = pathToAdd;
    usrPathsField.set(null, newPaths);
  }

  static
  {
    try {
      
      addLibraryPath("C:\\mydirA\\mydirB\\");
    } catch (Exception aE) {
      aE.printStackTrace();
    }
    
    // DLL name is myCPPlibrary.dll
    System.loadLibrary("myCPPlibrary");
  }
}

但是,我仍然得到

java.util.ServiceConfigurationError: com.test.wms.server.myfactory: could not be instantiated: java.lang.UnsatisfiedLinkError: C:\\mydirA\\mydirB\\\myCPPlibrary.dll: Can't find dependent libraries
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
Caused by: java.lang.UnsatisfiedLinkError: C:\\mydirA\\mydirB\\\myCPPlibrary.dll: Can't find dependent libraries
    at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)
    at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2442)
    at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2498)
    at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2694)
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2659)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
    ... 31 common frames omitted

I am writing a java web application which needs to load a native C++ dll via java source code. I have placed the dll called "myCPPlibrary.dll" and all the dependent DLLs at the same directory.
My function addLibraryPath ensures to add the path at which DLLs are located to add to java library path.

I was expecting this to work as main dll and dependent DLLs are all located in the same dir.

public class Test
{
  public static void addLibraryPath(String pathToAdd) throws Exception
  {
    final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    //get array of paths
    final String[] paths = (String[])usrPathsField.get(null);

    //check if the path to add is already present
    for(String path : paths) {
      if(path.equals(pathToAdd)) {
        return;
      }
    }

    //add the new path
    final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length-1] = pathToAdd;
    usrPathsField.set(null, newPaths);
  }

  static
  {
    try {
      
      addLibraryPath("C:\\mydirA\\mydirB\\");
    } catch (Exception aE) {
      aE.printStackTrace();
    }
    
    // DLL name is myCPPlibrary.dll
    System.loadLibrary("myCPPlibrary");
  }
}

However , i still get

java.util.ServiceConfigurationError: com.test.wms.server.myfactory: could not be instantiated: java.lang.UnsatisfiedLinkError: C:\\mydirA\\mydirB\\\myCPPlibrary.dll: Can't find dependent libraries
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
Caused by: java.lang.UnsatisfiedLinkError: C:\\mydirA\\mydirB\\\myCPPlibrary.dll: Can't find dependent libraries
    at java.base/java.lang.ClassLoader$NativeLibrary.load0(Native Method)
    at java.base/java.lang.ClassLoader$NativeLibrary.load(ClassLoader.java:2442)
    at java.base/java.lang.ClassLoader$NativeLibrary.loadLibrary(ClassLoader.java:2498)
    at java.base/java.lang.ClassLoader.loadLibrary0(ClassLoader.java:2694)
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2659)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
    ... 31 common frames omitted

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文