nar-maven-plugin 和 native-library-loader 不加载本机库

2023-12-31

我使用 nar-maven-plugin 进行测试,然后在下一个项目中我需要 JNI :( 。我选择了it0003 https://github.com/maven-nar/nar-maven-plugin/tree/master/src/it/it0003-jni来自 git 存储库的示例。如果没有本机库加载器,则手动放置库并设置其运行的库路径。然后我会使用本机库加载器。为此,我添加了单个 JAR 文件的依赖项和程序集插件。我当前的pom:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  #%L
  Native ARchive plugin for Maven
  %%
  Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->

<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>com.github.maven-nar.its.nar</groupId>
    <artifactId>it-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../it-parent/pom.xml</relativePath>
  </parent>

  <artifactId>it0003-jni</artifactId>
  <packaging>nar</packaging>

  <name>NAR JNI Test</name>
  <version>1.0-SNAPSHOT</version>
  <description>
    Simple JNI Library
  </description>
  <url>http://maven.apache.org/</url>

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

  <build>
    <defaultGoal>install</defaultGoal>
    <plugins>
      <plugin>
        <groupId>com.github.maven-nar</groupId>
        <artifactId>nar-maven-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <cpp>
            <debug>true</debug>
          </cpp>
          <c>
            <testOptions>
              <testOption>-DTESTOPT="this is a nar-testCompile flag"</testOption>
            </testOptions>
          </c>
          <libraries>
            <library>
              <type>jni</type>
              <narSystemPackage>it0003</narSystemPackage>
              <linkCPP>false</linkCPP>
            </library>
          </libraries>
          <javah>
            <includes>
              <include></include>
            </includes>
          </javah>
          <tests>
            <test>
              <name>HelloWorld</name>
            </test>
          </tests>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>it0003.HelloWorldJNI</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
      <dependency>
          <groupId>org.scijava</groupId>
          <artifactId>native-lib-loader</artifactId>
          <version>2.0.2</version>
      </dependency>
  </dependencies>
</project>

然后我将生成的 JAR 和 NAR 放在同一目录中,并以以下内容开头:

    rd4@PC222-VirtualBox ~/Schreibtisch/nartest $ java -Djava.library.path=/home/rd4/Schreibtisch/nartest/ -jar it0003-jni-1.0-SNAPSHOT-jar-with-dependencies.jar 

然后我收到以下错误:

java.lang.RuntimeException: Library 'libit0003-jni-1.0-SNAPSHOT.so' not found!
    at it0003.NarSystem.getLibPath(NarSystem.java:141)
    at it0003.NarSystem.loadLibrary(NarSystem.java:45)
    at it0003.HelloWorldJNI.<clinit>(HelloWorldJNI.java:27)
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Library 'libit0003-jni-1.0-SNAPSHOT.so' not found!
    at it0003.NarSystem.getLibPath(NarSystem.java:141)
    at it0003.NarSystem.loadLibrary(NarSystem.java:45)
    at it0003.HelloWorldJNI.<clinit>(HelloWorldJNI.java:27)

有谁知道我的错误是什么?


遇到了同样的问题。

确保您的 $JAVA_HOME 环境变量已设置。在 Linux 上,您可以使用以下命令找到它which java,然后使用导出路径export <path>。 此外,您应该链接到 pom.xml 文件中编译器选项中的包含路径:

<c>
  <name>gcc</name>
  <exceptions>false</exceptions>
  <debug>false</debug>
  <includes>
    <include>**/*.c</include>
  </includes>
  <options>
    <option>-I${java.home}/include</option>
    <option>-I${java.home}/include/linux</option>
  </options>
</c>

<linker>
  <name>gcc</name>
  <options>
    <option>-I${java.home}/include</option>
    <option>-I${java.home}/include/linux</option>
  </options>
</linker>

希望这会有所帮助,很确定这就是修复它的原因。如果我弄清楚到底是什么解决了这个问题,我会更新这个答案。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nar-maven-plugin 和 native-library-loader 不加载本机库 的相关文章

随机推荐