Como Chamar FORTRAN 95 de Java

1. Crie a aplicação FORTRAN 95:

subroutine f95_print(str, len)
  implicit none

  dll_export f95_print ! não-padrão

  character(len), intent(in) :: str

  integer, intent(in) :: len

  print *, str

end subroutine

2. Gere a DLL.

3. Crie a aplicação C:

Cabeçalho

#include <jni.h>

JNIEXPORT void JNICALL Java_JNISample_main(JNIEnv*, jclass, jobjectArray);

extern void JNICALL f95_print(const char*, const size_t*);

Fonte

#include <string.h>

#include "JNISample.h"

JNIEXPORT void JNICALL Java_JNISample_main(JNIEnv *env, jclass cls, 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);

    f95_print(chars, (size_t*)strlen(chars));

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

}

4. Gere a DLL.

5. Crie a aplicação Java:

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

6. 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