3

I am playing around with JNI and Maven to see how to automate the building process. I am encountering some difficulties on Linux platform when it comes to the output file name. When I run the maven build (below is the pom.xml for the linux platform), the shared library file name returned is jniExampleNative.so. If I try to run the java program with it I get java.lang.UnsatisfiedLinkError: no jniExampleNative in java.library.path. If I rename the file to libjniExampleNative.so it works.

I want to setup Maven so it automaticaly creates the file with the working file name, but don't know how. I tried setting linkerFinalName option (it is commented in the below pom.xml), but I get The packaging for this project did not assign a file to the build artifact.

Where am I doing things the wrong way and how can I set the output shared library's name?

<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>net.tricoder.jnitest</groupId>
        <artifactId>nativeParent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>net.tricoder.jnitest</groupId>
    <artifactId>jniExampleNative</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JNI example native Linux</name>
    <url>http://maven.apache.org</url>

    <packaging>so</packaging>

    <dependencies>
        <dependency>
            <groupId>net.tricoder.jnitest</groupId>
            <artifactId>jniExampleJava</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <!--  trigger javah -->
                    <javahOS>linux</javahOS>

                    <compilerProvider>generic-classic</compilerProvider>
                    <compilerExecutable>gcc</compilerExecutable>

                    <linkerExecutable>gcc</linkerExecutable>
                    <sources>
                        <source>
                            <directory>../src/main/native</directory>
                            <fileNames>
                                <fileName>jni_example.c</fileName>
                            </fileNames>
                        </source>
                    </sources>

                    <compilerStartOptions>
                        <compilerStartOption>-fPIC</compilerStartOption>
                    </compilerStartOptions>

                    <linkerStartOptions>
                        <linkerStartOption>-shared</linkerStartOption>
                    </linkerStartOptions>

                    <!--linkerOutputDirectory>${project.build.directory}</linkerOutputDirectory>
                    <linkerFinalName>libjniExampleNative</linkerFinalName-->

                </configuration>

                <executions>
                    <execution>
                        <id>javah</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <javahOS>linux</javahOS>
                            <javahProvider>default</javahProvider>
                            <javahOutputDirectory>${project.build.directory}/custom-javah</javahOutputDirectory>
                            <workingDirectory>${basedir}</workingDirectory>
                            <javahOutputFileName>NativeStuff.h</javahOutputFileName>
                            <javahClassNames>
                                <javahClassName>net.tricoder.jnitest.NativeStuff</javahClassName>
                            </javahClassNames>
                        </configuration>
                        <goals>
                            <goal>javah</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </build>
</project>
Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49

2 Answers2

4

the <linkerFinalName>libjniExampleNative</linkerFinalName> does the trick. The problem was with the lifecycles I was running mvn clean install -P linux. The install phase returned The packaging for this project did not assign a file to the build artifact.

After changing it to mvn clean package -P linux it works fine. Now I will have to figure out why install conflicts with <linkerFinalName>libjniExampleNative</linkerFinalName>, because I get the error only when I set the linkerFinalName option.

Alex Burdusel
  • 3,015
  • 5
  • 38
  • 49
1

Looking at the source code, I figured out that native-maven-plugin sets the build.finalName to <artifactId>, and ignores the original <finalName> and <linkerFinalName>: . So changing <artifactId> to what you want in the finalName "solves" the problem.

Azriel Berger
  • 160
  • 4
  • 17
hwvenancio
  • 11
  • 1