代码之家  ›  专栏  ›  技术社区  ›  ketan

如何使用所需的用户库执行maven main类?

  •  2
  • ketan  · 技术社区  · 8 年前

    我创建了一个maven项目,其中只有一个类可用。我想在这个类中使用jnetpcap API。为此,我遵循 jnet eclipse setup

    JnetTest。爪哇- 该类与 jnetpcap clasic example

    我使用的是openjdk版本“1.8.0_131”。

    库创建步骤-

    1. 创建了新库。Java->生成路径->用户库->新建->说出任何名字。
    2. 满足所需的依赖性。展开jar->来源->编辑->已选择lib/jnetpcap-src-1.3.zip。javadoc->编辑->选择lib/jnetpcap-javadoc-1.3.zip。本机库位置->编辑->选定的库目录-&燃气轮机;应用->好啊
    3. 将库添加到项目。右键单击项目->生成路径->配置生成路径->库->添加库->用户库->选择新建库。

    注释-

    在那之后,我右键单击我的项目并单击run as a java应用程序。这将在eclipse中正常工作。

    我想在一台只有命令行的不同机器上运行这个maven项目。如何使用命令行运行此项目?

    我的方法-

    1. <!-- 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)
    

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

    2 回复  |  直到 8 年前
        1
  •  1
  •   Ben Damer    8 年前

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

    1. 包含可以从代码中使用的Java接口的JAR文件(jnetpcap.JAR)
    2. 向Java库公开操作系统特定函数的本机库(即libjnetpcap.so或jnetpcap.dll)

    你可以通过回复 $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
    
        2
  •  1
  •   Ravi K    8 年前

    我没有你的确切代码,但我想你在找这个。

    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>