利用JNA使Java访问本地C++


(1)利用JNA使Java访问本地C++代码

 
  1. typedef int (*CB_OnServiceUserMessage)(uint32_t cid, uint32_t uid, const std::string& data);  
  2. extern "C"  
  3. {  
  4.    void init(CB_OnMsg cb_OnMsg)  
  5.    {  
  6.       (*cb_OnMsg)();  
  7.       std::cout << "Initializing..." << std::endl;  
  8.    }  
  9.    void start()  
  10.    {  
  11.       std::cout << "Start up!" << std::endl;  
  12.    }  
  13.    void stop()  
  14.    {  
  15.       std::cout << "Shut down!" << std::endl;  
  16.    }  
  17.    void send(char* msg)  
  18.    {  
  19.       std::cout << msg << std::endl;  
  20.    }  
  21. }  

(2)Java代码

PluginProxy.java

 
  1. import java.io.IOException;  
  2. import java.lang.management.ManagementFactory;  
  3.   
  4. import com.sun.jna.Callback;  
  5. import com.sun.jna.Library;  
  6. import com.sun.jna.Native;  
  7.   
  8. public class PluginProxy  
  9. {  
  10.     private static String libPath;      
  11.     private interface LibPluginProxy extends Library  
  12.     {  
  13.         static LibPluginProxy INSTANCE = (LibPluginProxy) Native.loadLibrary(libPath, LibPluginProxy.class);  
  14.         public void init(Callback callback);  
  15.         public void start();  
  16.         public void stop();  
  17.         public void send(String msg);  
  18.     }  
  19.   
  20.     public static void main(String[] args) throws Exception {  
  21.         libPath = "libPluginProxy.so";  
  22.         LibPluginProxy.INSTANCE.init(new CB_OnMsg());  
  23.         System.out.println("in main function.");  
  24.           
  25.         //add shutdown hook   
  26.         Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {  
  27.             @Override  
  28.             public void run() {  
  29.                 Native.unregister(LibPluginProxy.class);  
  30.                 System.out.println("stop..");  
  31.                 LibPluginProxy.INSTANCE.stop();  
  32.   
  33.                 String n = ManagementFactory.getRuntimeMXBean().getName();  
  34.                 if (n == nullreturn;  
  35.                 String pid = n.substring(0, n.indexOf("@"));  
  36.                 if (pid == nullreturn;  
  37.                 try  
  38.                 {  
  39.                     Runtime.getRuntime().exec("kill -9 " + pid);  
  40.                 }  
  41.                 catch (IOException e)  
  42.                 {  
  43.                     e.printStackTrace();  
  44.                 }  
  45.   
  46.                 System.exit(-1);  
  47.             }  
  48.         }));  
  49.         Thread t = new Thread(new Runnable() {  
  50.               
  51.             @Override  
  52.             public void run() {  
  53.                 LibPluginProxy.INSTANCE.start();  
  54.                 System.out.println("started in thread..");  
  55.             }  
  56.         });  
  57.           
  58.         t.start();    
  59.           
  60.         System.out.println("started!!");  
  61.     }  
  62. }  

CB_OnMsg.java

 
  1. import com.sun.jna.Callback;  
  2.   
  3. public class CB_OnMsg implements Callback {  
  4.   
  5.     // 应用创建   
  6.     public final int onMsg() {  
  7.         System.out.println("CB_OnMsg, in function onMsg.");  
  8.         return 0;  
  9.     }  
  10.       
  11. }  

(3)编译C++的.so共享库

编写makefile,细节忽略,只提供最主要的command,如下:

 
  1. g++ -shared -o libplugin.so -fPIC -Wall -ggdb -D_REENTRANT -DHAVE_EPOLL -DXML_NULL -DSTATUS_NEWPROTOCOL -Wl,-rpath,../bin  
  2. #注:此段有多余的flags,有待修改...  

(4)生成Java的JAR包

 在eclipse中生成JAR包,linux生成JAR命令见

 (5)运行JAR包调用.so共享库

 在linux下输入命令:java -cp jna-3.3.0.jar:ss.jar SalmonProxy 

相关内容