我创建了一个 Maven 项目,其中只有一个类可用。我想在这个类中使用 jnetpcap API。为此,我按照 jnet eclipse setup 教程使用设置 1 方法(用户库)创建了一个用户库并将其添加到我的项目中。
JnetTest.java -该类与jnetpcap经典示例相同
我的系统是Ubuntu 16.10。
我使用的是openjdk版本“1.8.0_131”。
库创建步骤 -
注意-我没有将 vm 参数添加到我的主类中。即 -Djava.library.path=".so 文件父目录的位置"
之后,我右键单击我的项目,然后单击“作为 java 应用程序运行”。这在 Eclipse 中可以正常工作。
实际问题-我想仅使用命令行在一台不同的机器上运行这个maven项目。我如何使用命令行运行这个项目?
我的方法 -
我在 pom.xml 中添加了以下插件用于主类配置。
<!-- language: lang-xml -->
<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.example.Main</mainClass>
</configuration>
</plugin>
我使用 mvn exec 命令来运行我的主类
mvn exec:java -Dexec.mainClass="com.example.Main"
但是我遇到了以下异常 -
Exception in thread "main" 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.(Unknown Source)
at com.slytechs.library.JNILibrary.(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.(Unknown Source)
我执行项目主类的方法是否正确?如果是的话,我的问题的解决方案是什么?如果没有,请建议有用的方法。
JnetPcap 要求您在项目中引用两个库:
您看到的异常表明您在运行时缺少#2。在这种情况下,您有两个选项可以使该库可供您的应用程序使用:
您可以通过回显
$PATH
系统变量来找出 Ubuntu 上的路径上有哪些目录:
> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
只需将本机库复制到系统路径中已包含的目录中即可。
或者,您可以使用
java.library.path
参数将库的位置传递给 Java。假设该库位于 Maven 项目目录中名为 lib
的目录中,请使用以下配置:
<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>
要执行此操作,只需运行:
mvn exec:exec
我没有你的确切代码,但我认为你正在寻找这个。
http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-using-plugin-dependency.html
下面标记的部分是您可以放置主类所需的依赖项的部分。我希望这能起作用。
<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>
您的错误是没有指定
java.library.path
值来加载库。
虽然Ben Damer的答案有效,但还有另一种方法:
MAVEN_OPTS='-Djava.library.path=./lib/' mvn exec:java -Dexec.mainClass="com.example.Main"
这可能很有用,例如,当您想使用
mvnDebug
来调试代码时。