简体   繁体   English

Java/C - 将本机语言代码编译成共享库 DLL

[英]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.在 Windows 操作系统中,我正在开发一个 maven java 应用程序,它将在新线程中使用 C 代码。

My java class that contains the call to the native method:我的 java 类包含对本机方法的调用:

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:要创建 .h 文件,我使用 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):在同一个 java 类包中,我在 C 中创建了实现本机代码 (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.现在我需要将本地语言代码 (NativeClass.c) 编译成共享库 writestring.dll 以匹配 System.loadLibrary 方法中使用的库名称。 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.我是 C 的初学者,我没有编译器,我不知道如何进行下一步来生成 DLL 并运行我的程序。 I would like support, if possible, to use maven for this task in a simple way.如果可能的话,我希望支持以简单的方式使用 maven 来完成这项任务。

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/我使用了链接https://expresscoding.wordpress.com/2016/05/23/using-native-maven-plugin/ 中的说明

My java class that contains the call to the native method:我的 java 类包含对本机方法的调用:

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:要创建 .h 文件,我使用 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生成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/我下载了 win64 的 GCC 编译器http://www.mingw.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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