- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
!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.

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