2017-07-20 26 views
1

我创建了一个只有一个类可用的maven项目。我想在这个类中使用jnetpcap API。为此,我遵循jnet eclipse setup教程与设置1方法(用户库),并创建一个用户库,并将其添加到我的项目。如何用所需的用户库执行maven主类?

JnetTest.java -这个类是相同jnetpcap clasic example

我的系统是Ubuntu的16.10。

我使用openjdk版本“1.8.0_131”。

图书馆创建步骤 -

  1. 我下载了1.3版本的jar包,源代码包和jnetpcap的javadoc的包,并添加libjnetpcap.so,jnetpcap.jar,jnetpcap-src-1.3.zip,jnetpcap-javadoc- 1.3.zip到在主项目文件夹下创建的lib文件夹。
  2. 建立了新的图书馆。 Java->构建路径 - >用户库 - >新建 - >给任何名称。
  3. 添加jar文件。添加外部jar - >工作区单选按钮 - >选定的lib/jnetpcap.jar
  4. 满足所需的依赖关系。展开jar - > source - > edit - >选中lib/jnetpcap-src-1.3.zip。 javadoc - >编辑 - >选定的lib/jnetpcap-javadoc-1.3.zip。本机库位置 - >编辑 - >选定的lib目录。 - >应用 - >确定
  5. 将库添加到项目中。右键点击项目 - >构建路径 - >配置构建路径 - >库 - >添加库 - >用户库 - >选择新创建的库。

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

之后,我右键单击我的项目,然后单击一个作为Java应用程序的运行。这将在日食中工作良好。

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

我的做法 -

  1. 我在下面的插件在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> 
    
  2. 我用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) 

我的做法是正确的或者不执行我的项目的主要课程?如果是,那么我的问题的解决方案是什么?如果没有,请提供有用的方法。

回答

1

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

  1. 包含您可以从您的代码(jnetpcap.jar)
  2. 暴露的机库使用Java接口JAR文件操作系统对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。假设该库是在一个名为lib你的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 
+0

@ BenDamer-感谢您的回答。我的问题是因为jar和dependency的版本冲突。任何方式您的解决方案都非常有帮助为什么版本1.3.0的maven dependency不可用?为什么我们需要从外部提供罐子? – kit

+0

版本1.3.0现在已经相当老旧,但1.4.x在Maven存储库中可用。或者您可以随时将1.3.0手动安装到存储库中。 –

+0

Hi @BenDamer,它工作的很好。我创建了像你一样的maven回答。现在我想在这个项目中增加一个主类而不会丢失这个结构。 – kit

0

我没有你的确切代码埠我认为你正在寻找这个。

http://www.mojohaus.org/exec-maven-plugin/examples/example-exec-using-plugin-dependencies.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> 
+0

@ RaviK-这依赖需要投入标签?我的项目只有一个依赖项artifact - jnetpcap,group - jnetpcap,版本 - 1.4.r1425-1d。我需要改变的是,请你详细解释一下。 – kit

+0

您可以尝试在依赖关系>依赖项部分中添加插件内的jnetpcap依赖项。当你通过exec插件运行java程序时,所有必要的依赖关系都会更好地在插件中给出。由于有限的信息有问题,这是我的猜测。 –

+0

@ RaviK-是的,我试过它没有任何影响。它没有得到libjnetpcap.so文件,所以它显示异常。我试着提供-Djava.library.path选项。 – kit

相关问题