How to execute maven main class with required user libraries?

前端 未结 2 1692
北恋
北恋 2021-01-23 08:11

I created one maven project in which only one class is available. I want to use jnetpcap API in this class. For this purpose, I followed jnet eclipse setup tutorial with Setup 1

2条回答
  •  甜味超标
    2021-01-23 08:36

    JnetPcap requires you to reference two libraries in your project:

    1. The JAR file containing the Java interfaces you can use from your code (jnetpcap.jar)
    2. The native library that exposes OS specific functions to the Java library (i.e., libjnetpcap.so or jnetpcap.dll)

    The exception that you are seeing indicates that you are missing #2 at runtime. In this case, you have two options to make the library available to your application:

    You can find out which directories are on your path on Ubuntu by echoing the $PATH system variable:

    > echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
    

    Simply copy the native library into a directory that is already included on the system path.

    Alternatively, you can pass the location of the library using the java.library.path argument to Java. Assuming the library is in a directory called lib inside your maven project directory, use the following configuration:

    
        org.codehaus.mojo
        exec-maven-plugin
        1.2.1
        
            java
            
                -Djava.library.path=${project.basedir}/lib
                -classpath
                
                com.example.Main
            
        
     
    

    To execute this, simply run:

    mvn exec:exec
    

提交回复
热议问题