The solution to the error of java using jni to call dll dynamic library Can't find dependent libraries

 When doing some java development recently, I need to call some things at the bottom of the operating system, so I wrote a dll dynamic library file in C++ and called it through java's JNI. After the dll file is generated, there is no problem in calling it with java on the local machine, but an error is reported when a host is changed. The error is as follows:

java.lang.UnsatisfiedLinkError: D:\workspace_bsp\myzauto\lib\getcpu.dll: Can't find dependent libraries。

       Probably means that the dependent library cannot be found.

       Since it is the first time to use java to call the dll dynamic library, I don't know how to solve this problem. I have searched the Internet for a long time with no results, and there is no solution on the Internet. Just know that the dependent dll is missing. But I don't know which dynamic libraries are missing. So I started trying to find a way to find the missing dependency library, and finally found a tool on the Internet that can check the dependency library of the dll you are using, such as mine (getcpu.dll).

The name of the tool is "DLL Dependency Viewing Tool". See nearby downloads.

        Unzip, double-click to open the DLL dependency viewing tool, and open your dll file (such as: getcpu.

The yellow question mark in the picture is the missing dll file, you download the missing dll file online (for example, my missing MSVCRTD.dll and MFC42D.dll).

Download address:   http://www.dllbang.com/ .

       After the download is successful, load it in, or put it in the system32 directory. That's it. Then use java to call getcpu.dll and it is successful.

        The code when the missing dll library file was not added before:

Java code   Favorite code
  1.         package com.bsp.zauto.common;  
  2.   
  3. public class JavaNativeCall {  
  4.   
  5.     static {    
  6.         System.load(System.getProperty("user.dir")+"/lib/getcpu.dll");     
  7.     }     
  8.       
  9.     public native int getCpuUsageRatio(int cputime);    
  10.  /** 
  11.   * @method main 
  12.   * @return void 
  13.   * @date 2011-9-12 06:39:19 PM 
  14.   * @param args 
  15.   * @description (description) 
  16.   */  
  17.  public static void main(String[] args) {  
  18.   // TODO Auto-generated method stub  
  19.   JavaNativeCall jc =  new  JavaNativeCall ();  
  20.   System.out.println(jc.getCpuUsageRatio(1000));  
  21.  }  
  22. }  

 

Running error:

java.lang.UnsatisfiedLinkError: D:\workspace_bsp\myzauto\lib\getcpu.dll: Can't find dependent libraries。

 

加载后代码:

Java代码   Favorite code
  1. package com.bsp.zauto.common;  
  2.   
  3. public class JavaNativeCall {  
  4.   
  5.     static {    
  6.         System.load(System.getProperty("user.dir")+"/lib/MSVCRTD.dll");    
  7.         System.load(System.getProperty("user.dir")+"/lib/MFC42D.dll");    
  8.         System.load(System.getProperty("user.dir")+"/lib/getcpu.dll");     
  9.     }     
  10.       
  11.     public native int getCpuUsageRatio(int cputime);    
  12.  /** 
  13.   * @method main 
  14.   * @return void 
  15.   * @date 2011-9-12 下午06:39:19 
  16.   * @param args 
  17.   * @decription (描述) 
  18.   */  
  19.  public static void main(String[] args) {  
  20.   // TODO Auto-generated method stub  
  21.   JavaNativeCall jc = new JavaNativeCall();  
  22.   System.out.println(jc.getCpuUsageRatio(1000));  
  23.  }  
  24. }  

 运行成功。

这个问题网上没有明确解决帖子,研究了半天才解决。写此文章,望大家少走弯路。

注意事项:

1:依赖库的顺序。

        System.load(System.getProperty("user.dir")+"/lib/MSVCRTD.dll");  
        System.load(System.getProperty("user.dir")+"/lib/MFC42D.dll");  
        System.load(System.getProperty("user.dir")+"/lib/getcpu.dll");  

     这个顺序须按照你的调用依赖库的顺序排列。 如果顺序写反了,依然会报 Can't find dependent libraries的错。

     Therefore, if you have loaded all the required dependent libraries and an error is reported, try to modify the order of these dependent libraries.

2: Download address of the dll file:

      http://www.dllbang.com/

      This website can search and download the dll dynamic library you need.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326010107&siteId=291194637