[JAVA] Posts that may be useful for JNI beginners

Introduction

It's a terrible title. I recently had the opportunity to come into contact with JNI, so as a reminder, ** "Until JNI beginners run native code (dll) on JNI" ** I would like to write easily.


Notice


Development environment


JNI What is #?

~~ Click the link to read. ~~ "The one that executes native code from Java" (appropriate)


Honestly JNI ...


Before using ...


You can write that code in Java

Later, if you get stuck with "Why did you use JNI to write in Java?", It may be that "It is better to write in Java without using JNI".


JNA or SWIG is (probably) easier to write

JNI needs to tweak the native code side. If you don't care about performance, consider JNA and SWIG. I can be happy. surely.


[Sad news] 64bit Java can't call 32bit dll

So let's match it properly. If you want to run on 64bit Java, use 64bit dll.


Does the native code support multithreading?

If not, tragedy may occur. Let's deal with it if necessary. If it's impossible, write a wrapper and control it exclusively ... (It's hard ...).


Finally use JNI


Calling Java source (JNISample.java)

package com.hoge.jnisample;

public class JNISample {
  //Native function declaration
  private native String nativeFunction(String name);

  //Load the module to run
  static {
    System.loadLibrary("libfuga");
  }

  //Native code execution
  public String executeNative(String name) {
    return this.nativeFunction(name);
  }
}

Generate class file (JNISample.class)

Compile the above source to generate a class file (JNISample.class).

javac JNISample.java

Generate native header

Use the javah command to generate a C (C ++) header. The command syntax is

javah -classpath {classpath} -d {Header output directory} {(package name.)name of the class}

This time, the directory structure is as follows.

java
└ com
  └ hoge
    └ jnisample
      ├ JNISample.java
      └ JNISample.class

The command looks like this: (Run in the java directory)

javah -classpath .\ -d ..\jniheader com.hoge.jnisample.JNISample

This will generate a header file in the jniheader directory, which is on the same level as the java directory.


Generated header file (com_hoge_jnisample_JNISample.h)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_hoge_jnisample_JNISample */

#ifndef _Included_com_hoge_jnisample_JNISample
#define _Included_com_hoge_jnisample_JNISample
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_hoge_jnisample_JNISample
 * Method:    nativeFunction
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_hoge_jnisample_JNISample_nativeFunction
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

All you have to do is implement the function in C (C ++) and build it!

but,


Last task: Add the path of the native module to the environment variable PATH


Now, execute

Did it work?


Summary

JNI, it's not surprisingly difficult, isn't it? It was a more difficult image, but I was relieved at Ponkotsuenji. We hope you find this post useful to all JNI beginners.


Editor's Note

Plenty of omissions in the second half. I honestly regret it. I wrote it for slide mode, but it's hard to read in normal mode.

Recommended Posts

Posts that may be useful for JNI beginners
Tools and commands that may be useful for Java troubleshooting
[Beginner] Commands and knowledge that may be useful for error resolution when deploying AWS
[For beginners] Let's be able to coat like Swift!