Passing an array from Fortran Function to JAVA, using JNA interface

Passing an array from Fortran Function to JAVA, using JNA interface

Hi all,I am really new with java. I would like to call from java a DLL compiled in Fortran and obtein in java the result array.I did with scalars and work well, but with array does not work.I will explain with a simple example:Fortran code, and compiled as DLL (F95Test.f)

function Proof(a)


integer, value :: a



DO i = 1, a

Proof(i) = 300


end function Proof

Now from Java code:

package Component;

import com.sun.jna.*;

public class Union {

interface F95Test extends com.sun.jna.Library {

F95Test lib = (F95Test) Native.loadLibrary("C:/Flood/FloodProgram/FloodProgram/Debug/FloodProgram.dll",


int[] FProof (int b);


public static void main(String[] args) {

int[] result=F95Test.lib.FProof(2);


}}When I try to run this code the message is:

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported return type class [I in function FProof

at com.sun.jna.Function.invoke(

at com.sun.jna.Function.invoke(

at com.sun.jna.Library$Handler.invoke(

at Component.$Proxy0.FProof(Unknown Source)

at Component.Union.main(

So I do not know if someone have moved arrays from fortran to java. I would appreciate so much any idea.



4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Steve Lionel (Intel)'s picture

Your Fortran function doesn't match the Java declaration. A Fortran function returning an array does so using a hidden first argument that is the return array. And in this case, where you declared it as being "adjustable" (with bounds specified by dummy argument a), Fortran expects that the caller has preallocated the return value array to that size and passed its address. I am not sure what the Java declaration means, but it certainly doesn't match.

Let me suggest that you stick to Fortran procedures that are "interoperable", which puts some restrictions on what the arguments and return value can be. Here, you may want to make it a subroutine and pass a preallocated array and the array size as arguments.


Thanks,That is true I was working in a wrong way.With subroutines it works fine.I copy both codes just in case it could be helpully for someone! Prueba.f90subroutine Prueba(arr, len) !DEC$ ATTRIBUTES DLLEXPORT::Prueba INTEGER, DIMENSION(len) :: arr INTEGER, VALUE :: len INTEGER:: iDO i=1, len arr(i)=arr(i)+30END DOwrite(*,*) arr(1)end subroutine PruebaFrom Javapackage Component;import com.sun.jna.*;public class Union { interface F95Test extends com.sun.jna.Library { F95Test lib = (F95Test) Native.loadLibrary("C://Prueba/Prueba/Prueba/Debug/Prueba.dll", F95Test.class); //------------Call Function/Subroutine as ARRAY ARGUMENTS--------------------- void PRUEBA(int[] arr, int len); } public static void main(String[] args) { //------------Call Function/Subroutine as ARRAY ARGUMENTS--------------------- int[] a = {1, 2, 3, 4, 5}; int b=a[0]; System.out.println(b); F95Test.lib.PRUEBA(a, a.length); int b1=a[1]; System.out.println(b1); } }Thanks,Miguel

hello sir.

i want tocall fortan function in m geting error.

here is my code:

public class FortanJava {
    interface  F95 extends com.sun.jna.Library
        F95 test=(F95)Native.loadLibrary("F90Dyn", F95.class);
         int foomult(int a, int b);

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        int result = F95.test.foomult(5,5);

m getting error like this Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library /tmp/jna5645755368766446105.tmp which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.

Login to leave a comment.