You are on page 1of 6

JNI invoking C (Programming Language) methods from

Java (Programming Language)

Author(s) of content of this article in References

Author of this article with improvements Rafael Peres dos Santos

21 December 2017
About the Author

Is graduated in Computer Science and is Senior Java Architect, Software Engineer, Senior
Linux Administrator, Senior Software Developer and has more than ten years of experience in
Java, Java Web projects and JEE projects and has these certifications: Sun Certified Java
Programmer, Sun Certified Web Component Developer, Oracle Certified Business
Component Developer and a lot of knowledge and know how. Is number one Software
Architect and Engineer in the World for most JEE projects and published some IT articles that
can be found in: https://independent.academia.edu/RafaelPeresdosSantos or in
https://www.scribd.com/lists/21789183/TI
Summary
JNI...............................................................................................................................................4
Building Java class.................................................................................................................4
Building C files........................................................................................................................5
References..................................................................................................................................6
JNI

JNI is Java API used to build C 'proxy' to Java invoke and interact with C methods. This
article describe how to build Java Class to invoke C methods. On methods that Java will
invoke C methods use native keyword and use javah to build C 'proxy' file, implement
something using C, compile and invoke Java class.

Building Java class

// Build a file with name: ClassName.java


// Content of ClassName.java
public class ClassName {

public native void methodToInvoke();

public static void main(String [] args) {

System.loadLibrary(“CClassName”);

ClassName className = new ClassName();

className.methodToInvoke();

javac ClassName
javah ClassName # to build C 'proxy' will build ClassName.h

Details:
• Invoking a class without build C proxy and implementation C method will result in
java.lang.UnsatisfiedLinkError
Building C files

// build a file with name: 'CClassName.h' with content:

#include <jni.h>

#include <stdio.h>

JNIEXPORT void JNICALL Java_ClassName_methodToInvoke (JNIEnv * env, jobject jobj)


{

printf(“method in C was invoked from Java”);


}

# use gcc to build .dll or .dylib or .so in directory that are in use
gcc -o libCClassName.so -shared -I $jdkPath/headers CClassName.c -lc

# invoke Java class in the same directory of C artifacts using java to invoke main method of
ClassName that will invoke methodToInvoke in C java proxy that will display “method in C was
invoked from Java”, don't forget the Details
java ClassName

# if using mvn copy the builded C files to /src/main/resources and add mvn tag:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
References

http://stuf.ro/calling-c-code-from-java-using-jni