Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling fortran from JAVA

Amrin_Fathima
Beginner
565 Views

I am trying to call below fortran routine from JAVA

C FortranCode.f

INTEGER FUNCTION SUMSQUAREDF(N,A)

INTEGER A(*)

WRITE(6,'("-- We are now in the FORTRAN program FortranCode --")')

WRITE(6,'("Print contents of array A() copied from a[] in C "

1 "program - note the unit offset")')

DO I=1,N

WRITE(6,'(2I5)')I,A(I)

ENDDO

WRITE(6,'("Calculate then print out squares of elements with "

1 "accumulated sums")')

ISUM=0

DO I=1,N

A(I)=A(I)*A(I)

ISUM=ISUM+A(I)

WRITE(6,'(3I5)')I,A(I),ISUM

ENDDO

SUMSQUAREDF=ISUM

END

my Javacode is like this

// JavaCode.java

class JavaCode {

final static int MAXSIZE = 10;

private native int sumsquaredc(int arr[]);

public static void main(String args[]) {

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

JavaCode c = new JavaCode();

int arr[] = new int[MAXSIZE];

System.out.println("Initialize the array arr[]");

for (int i=0; i

arr = i;

System.out.println(i + " " + arr);

}

System.out.println("Call the C code");

int sum = c.sumsquaredc(arr);

System.out.println("-- We are back in Java --");

System.out.println("Contents of arr[]");

for (int i=0; i

System.out.println(i + " " + arr);

System.out.println("Sum of squares in arr[] = " + sum);

System.out.println("Exit Java");

}

static {

System.loadLibrary("JavaCode");

}

}

and my mative code is

#include

#include "JavaCode.h" // Required header for JNI

// FORTRAN routines have to be prototyped as extern, and parameters are

// passed by reference. Note also that for g77 the function name in C by

// default must be prefixed by a "_".

extern int sumsquaredf_(int *, int []);

// When calling C code from Java, main() must be replaced by a declaration

// similar to below, where the function name is given by "Java_" + the name

// of the class in the Java code that calls this C code, in this case

// "JavaCode", + "_" + the name of this C function called from Java, in this

// case "sumsquaredc". This is followed by at least two parameters as below,

// plus possibly more if more are required.

JNIEXPORT jint JNICALL Java_JavaCode_sumsquaredc(JNIEnv *env,

jobject obj, jintArray ja) {

// Data from any additional parameters are passed via special pointers as

// shown here.

jsize n = (*env)->GetArrayLength(env, ja);

jint *a = (*env)->GetIntArrayElements(env, ja, 0);

int i,result;

printf("-- We are now in the C program CCode --\\n");

printf("Print contents of array a[] copied from arr[] in Java\\n");

for (i=0; i

printf("%2d %5d\\n",i,a);

printf("Call the FORTRAN code\\n");

// The data are passed to the FORTRAN program, then the results are returned

// in a way similar to this.

result = sumsquaredf_(&n,a);

printf("-- We have now returned back to the C program --\\n");

printf("Print contents of array a[]\\n");

for (i=0; i

printf("%2d %5d\\n",i,a);

printf("Sum of squares in array = %d\\n",result);

// Instead of ending as a normal C program, the pointers must be cleared

// before returning to Java.

(*env)->ReleaseIntArrayElements(env, ja, a, 0);

return result;

}

I am follwing below steps to compile

ifort -c sumsquaredf.f

cl sumsquaredf.obj sumsquaredc.c /I"C:\\Program Files\\Java\\jdk1

.6.0_21\\include" /I"C:\\Program Files\\Java\\jdk1.6.0_21\\include\\win32"

*******after this line it says unresolved external symbol sumsquaredf_ and main_****

Please suggest on how to link fortran and c,so that it creates proper dll.....
thanks a lot in advance

0 Kudos
1 Reply
Steven_L_Intel1
Employee
565 Views
The code you are using was clearly written to be used with the obsolete G77 compiler and Linux/UNIX naming conventions. Instead of calling "sumsquaredf_" you should probably call _SUMSQUAREDF instead. However, Java may not let you call a routine with a name beginning with an underscore. An alternative would be to add in the Fortran function, after the FUNCTION line, this:

!DEC$ ATTRIBUTES ALIAS:"sumsquaredf_" :: SUMSQUAREDF

Many years ago, our Lorri Menard wrote an article on calling Fortran from Java. Note that Lorri was writing about DVF/CVF and its STDCALL default - that part does not apply to Intel Fortran so the ATTRIBUTES C is not needed.
0 Kudos
Reply