本文介绍了如何用必需的用户库执行maven主类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个只有一个类可用的maven项目。我想在这个类中使用jnetpcap API。为此,我遵循了安装程序1(用户库)的教程,并创建了一个用户库将其添加到我的项目中。



JnetTest.java - 此类与



我的系统是Ubuntu 16.10。



m使用openjdk版本1.8.0_131。



图书馆制作步骤 -


  1. 我下载了1.3版本的jar包,源码包和javadoc的jnetpcap包,并添加了libjnetpcap.so,jnetpcap.jar,jnetpcap-src-1.3.zip,jnetpcap-javadoc-1.3.zip到主文件夹下创建的lib文件夹。 / li>
  2. 创建新库。 Java->构建路径 - >用户库 - >新建 - >任何名称。

  3. 添加jar文件。添加外部jar - > workspace单选按钮 - >选择lib / jnetpcap.jar

  4. 满足所需的依赖关系。扩展jar - > source - > edit - >选择lib / jnetpcap-src-1.3.zip。 javadoc - >编辑 - >选择lib / jnetpcap-javadoc-1.3.zip。本地库位置 - >编辑 - >选择lib目录。 - > APPLY - > OK

  5. 将库添加到项目中。右键单击项目 - >构建路径 - >配置构建路径 - >库 - >添加库 - >用户库 - >选择新创建的库。

注 - 我没有在我的主类中添加vm参数。即-Djava.library.path =位置到.so文件的父目录



之后,我右键单击我的项目,并单击运行作为一个java应用程序。这将在eclipse中工作正常。



实际问题 - 我想在一个不同的机器上运行这个maven项目,只有命令行。我如何使用命令行运行这个项目?



我的方法 -


  1. 我在pom.xml中添加了下面的插件,用于主类配置。

     <! - 语言:lang-xml  - > ; 
    < plugin>
    < groupId> org.codehaus.mojo< / groupId>
    < artifactId> exec-maven-plugin< / artifactId>
    < version> 1.2.1< / version>
    <执行>
    < execution>
    < goals>
    < goal> java< / goal>
    < / goals>
    < / execution>
    < / executions>
    < configuration>
    < mainClass> com.example.Main< / mainClass>
    < / configuration>
    < / plugin>


  2. 我使用mvn exec命令运行我的主要类

      mvn exec:java -Dexec.mainClass =com.example.Main


但是我得到以下异常 -

 线程main中的异常java.lang.UnsatisfiedLinkError:
com.slytechs.library.NativeLibrary.dlopen(Ljava / lang / String;)j
在com.slytechs.library.NativeLibrary。 (本地方法)
at com.slytechs.library.NativeLibrary。(未知来源)
at com.slytechs.library.JNILibrary。(Unknown Source)
at com.slytechs.library。 (未知来源)
at com.slytechs.library.JNILibrary.register(未知来源)
at com.slytechs.library.JNILibrary.register(Unknown Source)
at com。 (未知来源)
$ b

我的方法是正确的还是不执行我的项目的主类?如果是,那么解决我的问题是什么?如果没有,请建议有用的方法。

解决方案

JnetPcap要求您在项目中引用两个库:


  1. 包含可以从代码(jnetpcap.jar)使用的Java接口的JAR文件

  2. 向Java库公开操作系统特定功能的本地库(即libjnetpcap.so或jnetpcap.dll)

您看到的异常表明您在运行时缺少#2。在这种情况下,您有两个选项可以使您的应用程序可用:



您可以在Ubuntu上找到哪些目录在您的路径上,回显 $ PATH 系统变量:

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

只需将本机库复制到系统路径中已包含的目录。



或者,您可以使用 java.library.path 参数传递库的位置到Java。假设该库位于您的maven项目目录中的一个名为 lib 的目录中,请使用以下配置:

 <插件> 
< 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 


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 approach (user library) and created one user library and added it to my project.

JnetTest.java - This class is same as jnetpcap clasic example

My system is Ubuntu 16.10.

I'm using openjdk version "1.8.0_131".

Library creation steps -

  1. I downloaded 1.3 version jar package, source package and javadoc package of jnetpcap and added libjnetpcap.so, jnetpcap.jar, jnetpcap-src-1.3.zip, jnetpcap-javadoc-1.3.zip to lib folder created under main project folder.
  2. created new library. Java-> Build Path -> User Libraries -> New -> Give Any Name.
  3. Add jar file. Add external jars -> workspace radio button -> selected lib/jnetpcap.jar
  4. fulfill the required dependency. expand jar -> source -> edit -> selected lib/jnetpcap-src-1.3.zip. javadoc -> edit -> selected lib/jnetpcap-javadoc-1.3.zip. Native library location -> edit -> selected lib directory. -> APPLY -> OK
  5. add library to project. right click on project -> Build Path -> Configure Build Path -> Libraries -> Add Library -> User Library -> select newly created library.

Note - I didn't added vm argument to my main class. i.e. -Djava.library.path="location to parent directory of .so file"

After that I right click on my project and clicked on a run as a java application. This will work in eclipse fine.

Actual problem - I want to run this maven project on one different machine with command line only. How I can run this project using command line?

My approach -

  1. I added below plugin in pom.xml for main class configuration.

    <!-- 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>
    

  2. I used mvn exec command to run my main class

    mvn exec:java -Dexec.mainClass="com.example.Main"
    

But I got below exception -

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)

My approach is correct or not to execute my project's main class? If yes then what will be the solution to my problem? If no please suggest useful approach.

解决方案

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

这篇关于如何用必需的用户库执行maven主类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 10:22