Pages

Tuesday, November 27, 2012

MinGW / Java / JNI

File: Test.java

public class Test {
  public native void displayMessage();

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

  public static void main(String args[]) {
    new Test().displayMessage();
  }
}


File: TestImp.c


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

JNIEXPORT void JNICALL Java_Test_displayMessage(JNIEnv *env, jobject obj)
{
  printf("Testing 1 2 3...\n");

  return;
}


File: Makefile


JAVA_DIR=/c/Program\ Files/Java/jdk1.7.0_07

all:
@echo
@echo "Compiling Java source"

${JAVA_DIR}/bin/javac.exe Test.java

@echo
@echo "Generating header file"

${JAVA_DIR}/bin/javah.exe -jni Test

@echo
@echo "Creating DLL file"

gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I \
${JAVA_DIR}/include -I ${JAVA_DIR}/include/win32 \
-shared -o teste.dll TestImp.c

@echo
@echo "Executing..."

${JAVA_DIR}/bin/java.exe Test

clean:
rm -f Test.class
rm -f Test.h
rm -f Test.exe
rm -f teste.dll

No comments: