Java 如何使用所需的用户库执行maven main类?
我创建了一个maven项目,其中只有一个类可用。我想在这个类中使用JNETPCAPAPI。为此,我遵循教程中的setup1方法(用户库),创建了一个用户库并将其添加到我的项目中 JnetTest.java-此类与 我的系统是Ubuntu 16.10 我使用的是openjdk版本“1.8.0131” 库创建步骤-Java 如何使用所需的用户库执行maven main类?,java,eclipse,maven,jnetpcap,Java,Eclipse,Maven,Jnetpcap,我创建了一个maven项目,其中只有一个类可用。我想在这个类中使用JNETPCAPAPI。为此,我遵循教程中的setup1方法(用户库),创建了一个用户库并将其添加到我的项目中 JnetTest.java-此类与 我的系统是Ubuntu 16.10 我使用的是openjdk版本“1.8.0131” 库创建步骤- 我下载了jnetpcap的1.3版本jar包、源程序包和javadoc包,并将libjnetpcap.so、jnetpcap.jar、jnetpcap-src-1.3.zip、jnetp
<!-- 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>
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)
我的方法是否正确以执行项目的主类?如果是,那么我的问题的解决方案是什么?如果没有,请建议有用的方法 我没有你的确切密码,但我想你在找这个 下面标记的部分是可以放置主类所需依赖项的部分。我希望这能奏效
<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>
...
org.codehaus.mojo
execmaven插件
1.6.0
...
JAVA
假的
真的
com.example.myproject
迈里布
com.example.Main
论据1
...
我的财产
我的价值观
...
com.example.myproject
迈里布
1.3.5
罐子
...
JnetPcap要求您在项目中引用两个库:
$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>
@RaviK-哪个依赖项需要放在标签中?我的项目只有一个依赖项工件-jnetpcap,组-jnetpcap,版本-1.4.r1425-1d。我需要更改的内容请您详细解释。您可以尝试在dependencies>dependency部分的插件中添加jnetpcap依赖项吗。当您通过exec插件运行java程序时,最好在插件中给出所有必要的依赖项。由于信息有限,这是我经过计算的猜测。@RaviK-是的,我试过了,但没有效果。它没有获取libjnetpcap.so文件,因此显示异常。我也尝试过提供-Djava.library.path选项。您能否确保您的maven命令也使用与IDE完全相同的java版本。Maven通常会获取JAVA_主变量值。给maven提供正确的java,然后尝试-Djava.library.path选项。@RaviK-在我的系统中,只有java 1.8版本可用,命令行和eclipse都只使用相同的1.8版本。我检查maven也使用同样的Java1.8(java_HOME)。@BenDamer-谢谢你的回答。我的问题是因为jar和依赖项的版本冲突。不管怎样,你的解决方案都很有帮助。为什么版本1.3.0的maven依赖项不可用?为什么我们需要在外部提供jar?版本1.3.0现在已经相当旧了,但是1.4.x在Maven存储库中可用。或者你可以随时手动将1.3.0安装到你的存储库中。嗨@BenDamer,它工作得很好。我像你回答的那样创造了maven。现在我想在这个项目中再添加一个主类而不丢失这个结构。在这种情况下,请将我的答案标记为已接受。我建议您为下一步要做的事情创建一个新问题。
<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