Como Chamar C de Java

1. Crie a aplicação C:

#include <stdio.h>

#include <jni.h>

JNIEXPORT void JNICALL Java_JNISample_main(JNIEnv* env, jobject obj, jobjectArray args) {
  jsize sz;

  for (sz = 0; sz GetArrayLength(env, args); sz++) {
    const jstring str = (*env)->GetObjectArrayElement(env, args, sz);

    const char* chars = (*env)->GetStringUTFChars(env, str, JNI_FALSE);

    printf("%s\n", chars);

    (*env)->ReleaseStringUTFChars(env, str, chars);
  }

}

2. Gere a DLL.

3. Crie a aplicação Java:

public final class JNISample {
  public static native void main(final String[] args);
}

4. Execute a aplicação Java:

java JNISample Simples Assim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s