How to execute maven main class with required user libraries?

我的未来我决定 提交于 2019-12-02 03:53:40

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:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-Djava.library.path=${project.basedir}/lib</argument>
            <argument>-classpath</argument>
            <classpath />
            <argument>com.example.Main</argument>
        </arguments>
    </configuration>
 </plugin>

To execute this, simply run:

mvn exec:exec

I don't have your exact code bu I think you are looking for this.

http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-using-plugin-dependencies.html

Below marked section is the one where you can put dependencies required by your main class. I hope this works.

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
              <execution>
                ...
                <goals>
                  <goal>java</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <includeProjectDependencies>false</includeProjectDependencies>
              <includePluginDependencies>true</includePluginDependencies>
              <executableDependency>
                <groupId>com.example.myproject</groupId>
                <artifactId>mylib</artifactId>
              </executableDependency>
              <mainClass>com.example.Main</mainClass>
              <arguments>
                <argument>argument1</argument>
                ...
              </arguments>
              <systemProperties>
                <systemProperty>
                  <key>myproperty</key>
                  <value>myvalue</value>
                </systemProperty>
                ...
              </systemProperties>
            </configuration>
<!-- This is where you put dependencies needed for main class-->
            <dependencies>
              <dependency>
                <groupId>com.example.myproject</groupId>
                <artifactId>mylib</artifactId>
                <version>1.3.5</version>
                <type>jar</type>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
       ...
    </project>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!