2013-08-21 71 views
0

我是在Android項目中使用JNetPcap庫的堆棧: 即使我將JNetPcap設置爲庫該項目。未找到爲本地Lcom/slytechs/library/NativeLibrary實現的實現

有人可以幫助我嗎? PLZ

我的代碼如下:

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.SocketException; 
import java.net.UnknownHostException; 
import java.util.List; 

import org.jnetpcap.PcapIf; 
import org.jnetpcap.packet.JMemoryPacket; 
import org.jnetpcap.packet.JPacket; 
import org.jnetpcap.protocol.JProtocol; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Server_Android extends Activity{ 

    private final static int SERVER_PORT = 1234; 
// public final static int RECEIVING_TIMEOUT_SERVER = 3000; 
    DatagramSocket socket; 
    DatagramPacket packetOut; 
    DatagramPacket packetIn; 
    byte[] DataIn; 
    byte[] DataOut; 
    List<PcapIf> alldevs; 
    StringBuilder errbuf; 

    /*Android widgets*/ 
    TextView text; 
    EditText edit; 
    Button button; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_server_android); 
     //text = (EditText) 
     button =(Button)findViewById(R.id.button1); 
     edit = (EditText)findViewById(R.id.editText1); 
     text =(TextView)findViewById(R.id.textView2); 
     text.setText(""); 

     handlingpacket(); 

     /* Thread for receiving Data from CLient */ 
     new Thread(new Message_handler()).start(); 
      try { 
     Thread.sleep(500); 
     reply(); 
     }catch(InterruptedException e){ 
      Log.e("UDP", "UDP receive failed!"); 
     } 

    } 

// 
    public void handlingpacket(){ 

     JPacket jmp = new JMemoryPacket(JProtocol.UDP_ID, 
        "001801bf 6adc0025 4bb7afec 08004500 " 
       + " 0041a983 40004006 d69ac0a8 00342f8c " 
       + " ca30c3ef 008f2e80 11f52ea8 4b578018 " 
       + " ffffa6ea 00000101 080a152e ef03002a " 
       + " 2c943538 322e3430 204e4f4f 500d0a"); 
     text.append("" + jmp); 

    } 
// 
    public class Message_handler implements Runnable{ 

      public Message_handler(){} 

      public void run(){ 

      try { 
       DataOut = new byte[1024]; 

      while(true){ 

        socket = new DatagramSocket(SERVER_PORT); 
        socket.setReuseAddress(true); 
        socket.setBroadcast(true); 
     //   socket.setSoTimeout(RECEIVING_TIMEOUT_SERVER); 
        packetIn = new DatagramPacket(DataOut,DataOut.length);   
        socket.receive(packetIn); 

         runOnUiThread(new Runnable(){ // original thread for UI interactions 

           public void run(){ 
            String message = new String(packetIn.getData()); 
            text.append("Message " + message +"\n From :" + packetIn.getAddress() + " on port : "+ packetIn.getPort() +"\n"); 
            Log.d("UDP", "le message reçu"); 
            Log.d("Message : ", "" + message); 

           } 
         }); 


       } 
      } 
       catch(UnknownHostException exc) { 
        exc.printStackTrace(); 
       } 
       catch(SocketException exc) { 
        System.out.println("Problem openning socket"); 
       } 
       catch(IOException exc) { 
        System.out.println("Problem in reception of messages"); 
       } 

      socket.close(); 

      } 

     } 

     public void reply(){ 

      DataIn = new byte[1024]; 
      button.setOnClickListener(new Button.OnClickListener(){  

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         try { 

         socket = new DatagramSocket(); 
         String str = edit.getText().toString(); 
         DataIn = str.getBytes(); 
         packetOut = new DatagramPacket(DataIn, DataIn.length, packetIn.getAddress(), packetIn.getPort()); 
         socket.send(packetOut); 
         }catch (SocketException e1) { 
          // TODO Auto-generated catch block 
          e1.printStackTrace(); 
         } 
         catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       }); 

     } 
} 

日誌文件:

08-21 11:18:36.755: W/dalvikvm(776): No implementation found for native Lcom/slytechs/library/NativeLibrary;.dlopen (Ljava/lang/String;)J 
08-21 11:18:36.755: W/dalvikvm(776): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/jnetpcap/nio/JMemory; 
08-21 11:18:36.755: D/AndroidRuntime(776): Shutting down VM 
08-21 11:18:36.755: W/dalvikvm(776): threadid=1: thread exiting with uncaught exception (group=0x40a2d1f8) 
08-21 11:18:36.760: E/AndroidRuntime(776): FATAL EXCEPTION: main 
08-21 11:18:36.760: E/AndroidRuntime(776): java.lang.ExceptionInInitializerError 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.example.server_android.Server_Android.handlingpacket(Server_Android.java:66) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.example.server_android.Server_Android.onCreate(Server_Android.java:50) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.Activity.performCreate(Activity.java:4465) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.ActivityThread.access$600(ActivityThread.java:132) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.os.Looper.loop(Looper.java:137) 
08-21 11:18:36.760: E/AndroidRuntime(776): at android.app.ActivityThread.main(ActivityThread.java:4575) 
08-21 11:18:36.760: E/AndroidRuntime(776): at java.lang.reflect.Method.invokeNative(Native Method) 
08-21 11:18:36.760: E/AndroidRuntime(776): at java.lang.reflect.Method.invoke(Method.java:511) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
08-21 11:18:36.760: E/AndroidRuntime(776): at dalvik.system.NativeStart.main(Native Method) 
08-21 11:18:36.760: E/AndroidRuntime(776): Caused by: java.lang.UnsatisfiedLinkError: dlopen 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.NativeLibrary.dlopen(Native Method) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.NativeLibrary.<init>(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.JNILibrary.<init>(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.JNILibrary.register(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.JNILibrary.register(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at com.slytechs.library.JNILibrary.register(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): at org.jnetpcap.nio.JMemory.<clinit>(Unknown Source) 
08-21 11:18:36.760: E/AndroidRuntime(776): ... 16 more 
+0

'JMemoryPacket'正在創建異常。它可能未找到或您的代碼的來源不明。 –

+0

是的,但是如何讓它爲Java項目所知? –

回答

0

我只是糾正了這個問題,在我的應用程序。

如果您是從IDE運行該程序,請確保您的DLL和IDE都是32位或64位。

否則,將該DLL放在您要執行jar的相同文件夾中,或使用-Djava.library.path指定位置。

+0

不。這與DLL無關,因爲它是Android系統上的運行時錯誤,而Android不是Windows,它不使用DLL庫格式,而是它自己的Linux .so格式變體。 –