2017-07-21 11 views
0

コマンドラインを使用して私のmavenプロジェクトスタンドアロンクラスを実行したいと思います。ネイティブコードを含むJavaアプリケーションでコマンドラインを使用してMavenメインクラスを実行するにはどうすればよいですか?

JnetSampleTest.java -

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import org.jnetpcap.Pcap; 
import org.jnetpcap.PcapIf; 
import org.jnetpcap.packet.PcapPacket; 
import org.jnetpcap.packet.PcapPacketHandler; 

public class JnetSampleTest { 

    public static void main(String[] args) { 

     List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs 
     StringBuilder errbuf = new StringBuilder(); // For any error msgs 

     int r = Pcap.findAllDevs(alldevs, errbuf); 
     if (r == Pcap.NOT_OK || alldevs.isEmpty()) { 
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString()); 
      return; 
     } 

     System.out.println("Network devices found:"); 

     int i = 0; 
     for (PcapIf device : alldevs) { 
      String description = (device.getDescription() != null) ? device.getDescription() 
        : "No description available"; 
      System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description); 
     } 

     PcapIf device = alldevs.get(8); // We know we have atleast 1 device 
     PcapIf d = new PcapIf(); 

     System.out.printf("\nChoosing '%s' on your behalf:\n", 
       (device.getDescription() != null) ? device.getDescription() : device.getName()); 

     int snaplen = 64 * 1024; // Capture all packets, no trucation 
     int flags = Pcap.MODE_PROMISCUOUS; // capture all packets 
     int timeout = 10 * 1000; // 10 seconds in millis 
     Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf); 

     if (pcap == null) { 
      System.err.printf("Error while opening device for capture: " + errbuf.toString()); 
      return; 
     } 

     PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() { 
      public void nextPacket(PcapPacket packet, String user) { 
       System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n", 
         new Date(packet.getCaptureHeader().timestampInMillis()), packet.getCaptureHeader().caplen(), // Length 
         packet.getCaptureHeader().wirelen(), // Original length 
         user // User supplied object 
       ); 
      } 
     }; 

     pcap.loop(10, jpacketHandler, "jNetPcap rocks!"); 
     pcap.close(); 
    } 
} 

私はthis tutorialに従うことによってjnetpcapためのユーザーライブラリを作成し、プロジェクトに追加しました。日食では、このコードは、罰金を実行しているが、私は私がのpom.xmlで、サンプルコードの下に追加して、コマンドラインを使用して、このコードを実行したい

-

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>1.2.1</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>java</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <mainClass>com.JnetSampleTest.JnetSampleTest</mainClass> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

私は私のメインクラスを実行するには、このコマンドを試してみました -

mvn exec:java -Dexec.mainClass="com.JnetSampleTest.JnetSampleTest" 

私はこの例外を取得しています -

私はこのクラスを実行するにはどうすればよい
[WARNING] 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) 
    at java.lang.Thread.run(Thread.java:748) 
Caused by: java.lang.UnsatisfiedLinkError: com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J 
    at com.slytechs.library.NativeLibrary.dlopen(Native Method) 
    at com.slytechs.library.NativeLibrary.<init>(Unknown Source) 
    at com.slytechs.library.JNILibrary.<init>(Unknown Source) 
    at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source) 
    at com.slytechs.library.JNILibrary.register(Unknown Source) 
    at com.slytechs.library.JNILibrary.register(Unknown Source) 
    at com.slytechs.library.JNILibrary.register(Unknown Source) 
    at org.jnetpcap.Pcap.<clinit>(Unknown Source) 

コマンドラインを使用して?

+0

この[リンク](http://jnetpcap.com/node/953)を参照すると、この問題の原因となる可能性が指摘されています。また、この[あなたの質問](https://stackoverflow.com/questions/39048964/jnetpcap-java-lang-unsatisfiedlinkerror-com-slytechs-library-nativelibrary-dl)を見てください。 –

答えて

0

ネイティブライブラリがありません。 x86とx64の問題である可能性があります。

+0

@ fhossfel-私はプロジェクトディレクトリ/ lib /ディレクトリの下にネイティブlibjnetpcap.soライブラリを保持し、作成されたユーザライブラリにネイティブライブラリ用のパスを提供しました。 – kit

+0

どのようにパッチを提供しましたか? java.library.pathを設定しましたか? – fhossfel

+0

@ fhossfel-私は-Djava.library.pathを設定しようとしましたが、ユーザライブラリを作成するときにlibフォルダのパスを与えました。詳細についてはhttps://stackoverflow.com/questions/45220894/how-to-execute-maven-main-class-with-required-user-libraries/質問をご覧ください。 – kit

0

このタイプのエラーは、コードが探しているso/dllファイルが見つからないときに発生します。 Windowsのsystem32フォルダにjnetpcapの.dllファイルが保存されていることを確認してください。

IntelliJのプロセスは、jnetpcap.com/?q=eclipse(またはNetBeansの場合)と似ています。私はIntelliJに精通していませんが、あなたはプロジェクトのメインディレクトリにネイティブライブラリ(Linuxの.soファイル、Windowsの.dllファイル、Macの.dylibファイル)をコピーすることができます。

+0

私はすでにユーザーライブラリに.soファイルを提供しており、ユーザーライブラリをプロジェクトに追加しました。私は知っている.soファイルは創設されていないが、それはなぜそうであるか? – kit

関連する問題