简体   繁体   中英

Java / C - Compile the native language code into a shared library DLL

In the Windows operating system, I'm developing a maven java application that will make use of a C code in a new thread.

My java class that contains the call to the native method:

public class NativeClass implements Runnable {

    public native void writeString(String s);

    static {
        System.loadLibrary("writestring");
    }

    @Override
    public void run() {
        writeString("Hello");
    }

}

To create the .h file, I'm using jni-headers-maven-plugin:

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>com.alexkasko.maven</groupId>
                <artifactId>jni-headers-maven-plugin</artifactId>
                <version>1.0.6</version>
                <executions>
                    <!-- generate header for native methods -->
                    <execution>
                        <id>javah</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>javah</goal>
                        </goals>
                        <configuration>
                            <javahClass>com.foo.NativeClass</javahClass>
                            <javahOutputFilePath>${project.build.directory}/classes/com/foo/NativeClass.h</javahOutputFilePath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

In the same package of class java I created the class in C that implements the native code (NativeClass.c):

#include <jni.h>
#include "NativeClass.h"
#include <stdio.h>

JNIEXPORT void JNICALL 
Java_com_foo_NativeClass_writeString (JNIEnv *, jobject, jstring); 
{
    printf("the string is %s \n", jstring);
    return;
}

Now I need to compile the native language code (NativeClass.c) into a shared library writestring.dll to match the library name used in the System.loadLibrary method. I am a beginner in C, I have no compiler and I have no idea how to proceed the next steps to generate the DLL and run my program. I would like support, if possible, to use maven for this task in a simple way.

thanks a lot!

With the following adjustments, everything will work. I used the instructions in the link https://expresscoding.wordpress.com/2016/05/23/using-native-maven-plugin/

My java class that contains the call to the native method:

public class NativeClass implements Runnable {

    public native void writeString(String s);

    static {
        System.loadLibrary("writestring");
    }

    @Override
    public void run() {
        writeString("Hello");
    }

}

To create the .h file, I'm using jni-headers-maven-plugin:

<build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>com.alexkasko.maven</groupId>
                <artifactId>jni-headers-maven-plugin</artifactId>
                <version>1.0.6</version>
                <executions>
                    <!-- generate header for native methods -->
                    <execution>
                        <id>javah</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>javah</goal>
                        </goals>
                        <configuration>
                            <javahClass>com.foo.NativeClass</javahClass>
                            <javahOutputFilePath>${project.build.directory}/classes/com/foo/NativeClass.h</javahOutputFilePath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

To generate the DLL

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <version>1.0-alpha-9</version>
                <extensions>true</extensions>
                <configuration>            
                    <javahOS>win32</javahOS>
                    <sources>
                        <source>
                            <directory>src/main/native</directory>
                            <fileNames>
                                <fileName>NativeClass.c</fileName>
                            </fileNames>
                        </source>
                        <source>
                            <directory>src/main/native/include</directory>
                        </source>
                    </sources>
                    <compilerProvider>generic-classic</compilerProvider>
                    <compilerExecutable>gcc</compilerExecutable>
                    <compilerStartOptions>
                        <compilerStartOption>-m64</compilerStartOption>
                        <compilerStartOption>-fpic</compilerStartOption>
                        <compilerStartOption>-Wall</compilerStartOption>
                        <compilerStartOption>-Wextra</compilerStartOption>
                        <compilerStartOption>-ansi</compilerStartOption>
                        <compilerStartOption>-g</compilerStartOption>
                        <compilerStartOption>-O3</compilerStartOption>
                    </compilerStartOptions>
                    <linkerOutputDirectory>target</linkerOutputDirectory>
                    <linkerExecutable>gcc</linkerExecutable>
                    <linkerStartOptions>
                        <linkerStartOption>-m64</linkerStartOption>
                        <linkerStartOption>-shared</linkerStartOption>
                    </linkerStartOptions>
                    <linkerFinalName></linkerFinalName>
                    <linkerEndOptions>
                        <linkerEndOption>-o ${project.build.directory}/writestring.dll</linkerEndOption>
                    </linkerEndOptions>
                </configuration>
                <executions>
                    <execution>
                        <id>javah</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>initialize</goal>
                            <goal>compile</goal>
                            <goal>link</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


        </plugins>
    </build>

NativeClass.c

#include <jni.h>
#include "NativeClass.h"
#include <stdio.h>

JNIEXPORT void JNICALL 
Java_com_foo_NativeClass_writeString (JNIEnv *env, jobject obj, jstring s)
{
    printf("the string is %s \n", jstring);
    return;
}

I downloaded the GCC compiler for win64 http://www.mingw.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM