Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Calling Java from Fortran using JNI

$
0
0

Hello,

I have a program to call Fortran from Java. The codes for func.f95, addC.c, and addJava.java are as follows:

FUNCTION  add(c, iflag) RESULT(f)
INTEGER, INTENT(IN):: c
INTEGER, INTENT(OUT):: iflag
INTEGER:: f
INTEGER:: i

i=10
f=c+i

PRINT *, f
PRINT *, iflag
END FUNCTION

#include <stdio.h>
#include "addJava.h"

extern int add(int *, int *);

JNIEXPORT jint JNICALL Java_addJava_add(JNIEnv *env,
                       jobject obj, jint c, jint iflag) {

    int result;

    printf("-- We are now in the C program CCode --\n");
    printf("c=%d, iflag=%d\n",c,iflag);

    printf("Call the FORTRAN code\n");
    result = add(&c,&iflag);

    printf("-- We have now returned back to the C program --\n");
    printf("c=%d, iflag=%d\n",c,iflag);

    printf("Result = %d\n",result);
    return result;
}
import java.io.*;
import java.lang.*;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.ptr.IntByReference;


class addJava {

    private native int add(int element, int iflag);

    public static void main(String args[]) {
	Native.setProtected(true);

	System.out.println(System.getProperty("jna.library.path"));
	System.out.println(System.getProperty("java.library.path"));
	System.load("/home/chaitra/Research/JAVA/examples/JNI/add/add.so");

        System.out.println("-- We are in the Java program JavaCode --");
        addJava result = new addJava();

        System.out.println("Call the C code");
        int sum = result.add(10, 0);

        System.out.println("-- We are back in Java --");
        System.out.println("Result = " + sum);
        System.out.println("Exit Java");
    }
}

I want to know how to call Java from Fortran i.e., Fortran -> C -> Java. I want to pass the value of variable c to a Java program that computes result=c+10 and returns the value to Fortran. How can this be done? Please help!


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>