简体   繁体   English

如何在包含本机代码的Java应用程序中使用命令行执行Maven主类?

[英]How to execute Maven main class using command line in a Java application containing native code?

I want to execute my maven project standalone class using command line. 我想使用命令行执行我的Maven项目独立类。

JnetSampleTest.java - 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();
    }
}

I created user library for jnetpcap by following this tutorial and added it to project. 通过遵循本教程 ,我为jnetpcap创建了用户库,并将其添加到项目中。

In eclipse this code is running fine but I want to execute this code using command line so I added below sample code in pom.xml - 在eclipse中,此代码运行良好,但我想使用命令行执行此代码,因此我在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>

I tried this command to execute my main class - 我尝试使用此命令执行我的主类-

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

I'm getting this exception - 我遇到这个例外-

[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)

How I can execute this class using command line? 如何使用命令行执行此类?

The native library is missing. 本机库丢失。 Could be a x86 vs. x64 issue. 可能是x86与x64的问题。

this type of error occurs, when the so/dll file that the code is looking for is not found. 当找不到代码正在寻找的so / dll文件时,会发生此类错误。 please ensure you have the .dll file for jnetpcap saved in the windows system32 folder. 请确保在Windows system32文件夹中保存了jnetpcap的.dll文件。

The process for IntelliJ should be similar to that on jnetpcap.com/?q=eclipse (or for NetBeans). IntelliJ的过程应与jnetpcap.com/?q=eclipse(或NetBeans)上的过程相似。 I'm not familiar with IntelliJ, but ... you could just try copying the native library (.so file on linux, .dll file on windows, .dylib file on Mac) into the main directory of your project. 我对IntelliJ不熟悉,但是...您可以尝试将本机库(Linux上的.so文件,Windows上的.dll文件,Mac上的.dylib文件)复制到项目的主目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM