Linking a static library to Java using JNI


Beginning JNI Linux tutorial for Netbeans

Once I had accomplished the above with complete success, I extended the experiment by linking my static library (.a) to the dynamic library, and calling the static library function from the dynamic library function.

My HelloWorldNative.h:
/* DO NOT EDIT THIS FILE – it is machine generated */
#include <jni.h>
/* Header for class helloworld_Main */

#ifndef _Included_helloworld_Main
#define _Included_helloworld_Main
#ifdef __cplusplus
extern “C” {
#endif
/*
* Class: helloworld_Main
* Method: nativePrint
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_helloworld_Main_nativePrint
(JNIEnv *, jobject);

/*
* Class: helloworld_Main
* Method: nativePrintNumber
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_helloworld_Main_nativePrintNumber
(JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

My HelloWorldNative.c:
#include <jni.h>

#include <stdio.h>

#include “../HelloWorldNative.h”

void hello();
void hello_number(int i);

JNIEXPORT void JNICALL Java_helloworld_Main_nativePrint(JNIEnv *env, jobject obj)
{
hello();
}

JNIEXPORT void JNICALL Java_helloworld_Main_nativePrintNumber
(JNIEnv *env, jobject obj, jint ji)
{
hello_number(ji);
}

My Main.java:
/*
* Main.java
*
* Created on February 9, 2007, 3:48 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package helloworld;

/**
*
* @author thomas
*/
public class Main {

private native void nativePrint();
private native void nativePrintNumber(int i);
/** Creates a new instance of Main */
public Main() {
}

static {
System.load(“/home/thomas/src/c/testlib/HelloWorldNative/dist/HelloWorldNative.so”);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main me = new Main();
me.nativePrint();
me.nativePrintNumber(2);
me.nativePrintNumber(5);
// TODO code application logic here
}

}


2 responses to “Linking a static library to Java using JNI”

  1. Can you send sample code to link static library in Java ….I am facing problem of java.lang.UnsatisfiedLinkError: no libaddition.a in java.library.path

    So could you help in resolving !!!!!!!

Leave a Reply

%d bloggers like this: