- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I can't compile your C driver. Can you please attach addJava.h?
Patrick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Since I needed to modify the java code to reflex my system path, I just generated my own addJava.h, so there's no need to attach your version (I only wanted to see what your java compiler produced for the C header).
I found the easiest way to do this exercise was simply to put the C driver and Fortran function in a shared object, and then make that available for java execution.
Incidently, in the Fortran function, regarding 'INTEGER, INTENT(OUT):: iflag', iflag is modified, so I just made it INTENT(IN).
Otherwise, the compiler complains:
[U544731]$ ifort -c add-orig.f90
add-orig.f90(1): warning #6843: A dummy argument with an explicit INTENT(OUT) declaration is not given an explicit value. [IFLAG]
FUNCTION add(c, iflag) RESULT(f)
-----------------^
[U544731]$
This now all seems to work fine, considering the above changes.
[U544731]$ export LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH
[U544731]$java addJava
-- We are in the Java program JavaCode --
Call the C code
-- We are now in the C program CCode --
c=10, iflag=0
Call the FORTRAN code
20
0
-- We have now returned back to the C program --
c=10, iflag=0
Result = 20
-- We are back in Java --
Result = 20
Exit Java
[U544731]$
Hope this helps.
Patrick

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page