5

我建立了一个多模块 maven 项目,它由一个用于构建 nar jni 库的模块和一个依赖于该库的 jar 打包模块组成。

我能够将 nar 库安装到我的本地 maven 存储库,但我无法在依赖模块中使用它。

例如,我运行mvn nar:nar-unpack并得到:

[INFO] ------------------------------------------------------------------------
[INFO] Building nar-dependent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- nar-maven-plugin:3.2.0:nar-unpack (default-cli) @ nar-dependent ---
[INFO] Unpacking 0 dependencies to /home/przemek/Documents/stimulant/nar-dependent/target/nar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

似乎没有nar依赖,这显然不是真的。此外,尝试执行使用 jni 库的类的 main 方法失败:

mvn exec:java -Dexec.mainClass=App

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ nar-dependent ---
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at jnibook.NarSystem.loadLibrary(NarSystem.java:23)
    at jnibook.HelloWorld.<clinit>(HelloWorld.java:10)
    at App.main(App.java:9)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

项目的结构如下所示:

.
├── nar-dependent
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── App.java
├── nar-library
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── c
│       │   │   └── HelloWorld.c
│       │   ├── include
│       │   ├── java
│       │   │   └── jnibook
│       │   │       └── HelloWorld.java
│       │   └── resources
│       └── test
│           └── java
├── parent
│   └── pom.xml

这是父 pom.xml:

 <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sidec</groupId>
    <artifactId>stimulant</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>../nar-library</module>
        <module>../nar-dependent</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

nar-library 模块 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
      <groupId>sidec</groupId>
      <artifactId>stimulant</artifactId>
      <version>1.0-SNAPSHOT</version>
      <relativePath>../parent/pom.xml</relativePath>
  </parent>


  <artifactId>nar-library</artifactId>
  <packaging>nar</packaging>

  <name>nar-library</name>

    <properties>
        <skipTests>true</skipTests>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions>
                <configuration>
                    <cpp>
                        <exceptions>false</exceptions>
                    </cpp>
                    <libraries>
                        <library>
                            <type>jni</type>
                            <linkCPP>false</linkCPP>
                            <narSystemPackage>jnibook</narSystemPackage>
                        </library>
                    </libraries>
                    <javah>
                        <includes>
                            <include></include>
                        </includes>
                    </javah>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

nar 依赖的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
      <groupId>sidec</groupId>
      <artifactId>stimulant</artifactId>
      <version>1.0-SNAPSHOT</version>
      <relativePath>../parent/pom.xml</relativePath>
  </parent>


  <artifactId>nar-dependent</artifactId>
  <packaging>jar</packaging>

  <name>nar-dependent</name>


    <build>
        <plugins>
            <plugin>
                <groupId>com.github.maven-nar</groupId>
                <artifactId>nar-maven-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions>
                <!--<executions>-->
                    <!--<execution>-->
                        <!--<id>nar-download</id>-->
                        <!--<goals>-->
                            <!--<goal>nar-download</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                <!--</executions>-->
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>sidec</groupId>
            <artifactId>nar-library</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>nar</type>
        </dependency>
    </dependencies>

</project>

最后,作为证明它确实是 HelloWorld 项目的一个库类:

package jnibook;

public class HelloWorld {
    public native void print();


    static {
        NarSystem.loadLibrary();
    }
} 

和客户端应用程序:

import jnibook.HelloWorld;

public class App {

    public static void main(String ... args){
        (new HelloWorld()).print();

    }
}

我引用了https://maven-nar.github.io/examples.html没有成功。我不知道出了什么问题。

有任何想法吗?这是带有项目的 zip

4

2 回答 2

5

这可能是一个过时的问题,但我会回答都是一样的:

运行 java JNI 应用程序时,必须告诉它在哪里可以找到保存 JNI 使用的相关本机 C 代码的 .so 库。例如,如果您在可执行 jar app.jar 中关闭了您的应用程序:

java -Djava.library.path=[path to the .so native C library] -jar app.jar

PS-由于异常,您可以看到 JVM 找不到本机 C 库:java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path

于 2015-09-10T13:03:37.470 回答
0

我已经使用插件的 3.6.0 版本尝试了我自己的示例变体。有了那个版本,我至少得到

    将 1 个依赖项解压到 /home/karsten/svn/hellotest/target/nar

.so当我mvn nar:nar-unpack在依赖模块中运行时,它们会被解包。但是我发现mvn nar:nar-integration-test在依赖模块中工作的唯一方法是编写

    LD_LIBRARY_PATH=target/nar/hellojni-0.0-amd64-Linux-gpp-jni/lib/amd64-Linux-gpp/jni mvn nar:nar-integration-test

我尝试了几种指定方法java.library.path,但没有成功。

于 2019-08-12T12:43:14.147 回答