Java.dllļ


1½JNative

    jnative.sourceforge.net/ JNativeԴĿ,ص1.3.2


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

    ļֱǣJNativeCpp.dlllibJNativeCpp.soJNative.jar  
    JNativeCpp.dll Windowsõģwindows / system32Ŀ¼£
    libJNativeCpp.so LinuxµģϵͳĿ¼£
    JNative.jarһչ빤LIBл俽jdk\jre\lib\ext £ϵͳԶء

3ʵ˵

    ҵĿʹJNativeһӦ÷״̬TestAppSvr.dllļ,DllļаһTestConnect(),һεĽ(10)

    úJNativewindows:
    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.dllJre\binĿ¼,JVMClassloaderʱԶ,Ȼͨ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.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\binTomcat\binĿ¼µıӿ

System.load():ݾĿ¼ӽرӿ,Ǿ·


 

4ע

   ʾ,Ϊ,ûд,ֻʵװDLLļ,DLLļ,Ϣ.

   JNativeϸ˵,οJNativeԴ.

   עJVMֻһĬϵClassLoaderload native libraryͬʱṩרŵAPIunloadһloaded native library,ĿԵʱ,Web Server.

