Java調用.dll檔案的方法_java

來源:互聯網
上載者:User

下截JNative組件

jnative.sourceforge.net/ 到這裡下載JNative開源項目,我下載的是1.3.2

解壓JNative-<st1:chsdate isrocdate="False" islunardate="False" day="30" month="12" year="1899">1.3.2</st1:chsdate>.zip

獲得三個檔案,分別是:JNativeCpp.dll,libJNativeCpp.so,JNative.jar 。
JNativeCpp.dll Windows下用的,拷貝到windows / system32目錄下;
libJNativeCpp.so Linux下的,拷貝到系統目錄下;
JNative.jar 這是一個擴充包,匯入工程LIB中或將其拷貝到jdk\jre\lib\ext 下,系統會自動載入。

•使用說明

我的項目將使用JNative組件調用一個測試應用伺服器狀態的TestAppSvr.dll檔案,Dll檔案中包含一個TestConnect()方法,返回一個整形的結果(1或0)

首先配置好JNative組件的windows環境:
將Native要用到JNativeCpp.dll放在系統硬碟的\WINDOWS\system32下

將JNative.jar匯入工程中,建立一個調用類:

java 代碼

複製代碼 代碼如下:

package com.tvjody;  

import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  

import org.xvolks.jnative.JNative;  
import org.xvolks.jnative.Type;  
import org.xvolks.jnative.exceptions.NativeException;  

public class AppSvrTestConnect {  

    public AppSvrTestConnect() {  

    }  

    /** 
     * 測試應用伺服器串連狀態 
     *  
     *  TestConnect  
     * @param ip 應用伺服器IP 
     * @param port 連接埠 
     * @param intrcpt  是否採用資料壓縮方式 1 :true 0:false 
     * @return int 1 :成功 0:失敗 
     * @throws NativeException 
     * @throws IllegalAccessException 
     */ 
    private static final int TestConnect(String ip, int port, int intrcpt)throws NativeException, IllegalAccessException {  
        JNative n = null;  
        try {             
            n = new JNative("TestAppSvr.dll", "TestConnect");  
            n.setRetVal(Type.INT);  
            int i = 0;  
            n.setParameter(i++, Type.STRING, ip);  
            n.setParameter(i++, Type.INT, "" + port);  
            n.setParameter(i++, Type.INT, "" + intrcpt);  
            n.invoke();  
            return Integer.parseInt(n.getRetVal());  
        } finally {  
            if (n != null)  
                n.dispose();  
        }  
    }  
    /** 
     * 指定Dll檔案路徑,動態載入本地連結庫,測試應用伺服器串連狀態 
     * setDllPath 
     * @param path Dll檔案的路徑,不包含DLL名稱 例如:windows - d:\test\test\ unix - root/test/test/ 
     * @param ip 應用伺服器IP 
     * @param port 連接埠 
     * @param intrcpt  是否採用資料壓縮方式 1 :true 0:false 
     * @return int 1 :成功 0:失敗 
     * @throws NativeException 
     * @throws IllegalAccessException 
     */ 
    public static final int TestConnectFromDllPath(String path,String ip, int port, int intrcpt) throws NativeException, IllegalAccessException{  
        path += "TestAppSvr.dll";  
        System.load(path);  
        return TestConnect(ip,port,intrcpt);  
    }  
    /** 
     * Dll檔案放在JRE\bin目錄下面,ClassLoader就能通過System.loadLibrary()動態載入本地連結庫 
     * TestConnectFromDllPath 
     * @param ip 應用伺服器IP 
     * @param port 連接埠 
     * @param intrcpt  是否採用資料壓縮方式 1 :true 0:false 
     * @return int 1 :成功 0:失敗 
     * @throws NativeException 
     * @throws IllegalAccessException 
     */ 
    public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) throws NativeException, IllegalAccessException{  
        System.loadLibrary("TestAppSvr");  
        return TestConnect(ip,port,intrcpt);  
    }  
}

這個類實現了一個靜態私人方法,用來調用Dll檔案中的方法返回結果

private static final int TestConnect(String ip, int port, int intrcpt)

兩個靜態公用方法,分兩種方式裝載DLL檔案

public static final int TestConnectFromDllPath(String path,String ip, int port, int intrcpt)  //通過DLL檔案的路徑
public static final int TestConnectFromDllPath(String ip, int port, int intrcpt) //通過ClassLoader             然後建立一個類,調用AppSvrTestConnect.java,實現方法一調用,我是將TestAppSvr.dll檔案與Demo.java放在一個目錄下 ,所以得到Demo.java的路徑後就可以得到TestAppSvr.dll的路徑,調用AppSvrTestConnect.TestConnectFromDllPath()方法後就能返回正確的資訊.方法二是已經將TestAppSvr.dll放在了Jre\bin目錄下,在JVM的Classloader的時候會自動載入,然後通過System.loadLibrary("TestAppSvr")就可以裝配DLL檔案.

java 代碼

複製代碼 代碼如下:

public class Demo {  
    public int getInfo() throws NativeException, IllegalAccessException{  

        String path=getClass().getResource(File.separator).getPath();         
        path = path.substring(1,path.length());  
        System.out.println(path);   //得到DLL檔案的路徑  

        String ip = "192.168.0.48"; //伺服器IP  
        int port = 221;             //連接埠  
        int intrcpt = 1;            //資料壓縮方式傳送,1為採用;0為不採用  
        //方法1 傳入Dll檔案的路徑  
        //int info = AppSvrTestConnect.TestConnectFromDllPath(path, ip, port, intrcpt);  

        //方法2 Dll檔案已經放在JRE\bin目錄下面  
        int info = AppSvrTestConnect.TestConnectFromDllPath(ip, port, intrcpt);  

        //1為成功,0為失敗  
        if (info == 1)  
            System.out.println("應用伺服器可用。");  
        else 
            System.out.println("應用伺服器不可用,請檢查IP地址和連接埠是否正確。");  

        return info;  
    }  

System.loadLibrary():裝載Windows\System32下或jre\bin或Tomcat\bin目錄下的本地連結庫

System.load():根據具體的目錄來加截本地連結庫,必須是絕對路徑

•備忘

上面的樣本工程,因為是例子,所以沒有大多的設計,只是實現了裝載DLL檔案,調用DLL檔案方法,返回資訊.

JNative的詳細說明,請參考JNative的來源程式和例子.

注意JVM只允許一個預設的ClassLoader來load native library,同時並不提供專門的API來unload一個loaded native library,所以在項目調試的時候,獨立啟動Web Server.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.