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

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

miguel_campo
Beginner
1,729 Views
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)

!DEC$ ATTRIBUTES DLLEXPORT,ALIAS:"FProof" :: Proof

integer, value :: a

INTEGER, DIMENSION (a) ::Proof

integer::i

DO i = 1, a

Proof(i) = 300

END DO

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",

F95Test.class);

int[] FProof (int b);

}

public static void main(String[] args) {

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

System.out.println(result);

}
}
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(Function.java:424)

at com.sun.jna.Function.invoke(Function.java:276)

at com.sun.jna.Library$Handler.invoke(Library.java:216)

at Component.$Proxy0.FProof(Unknown Source)

at Component.Union.main(Union.java:46)

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

Thanks,

Miguel

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,729 Views
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.
0 Kudos
miguel_campo
Beginner
1,729 Views
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.f90
subroutine Prueba(arr, len)
!DEC$ ATTRIBUTES DLLEXPORT::Prueba
INTEGER, DIMENSION(len) :: arr
INTEGER, VALUE :: len
INTEGER:: i
DO i=1, len
arr(i)=arr(i)+30
END DO
write(*,*) arr(1)
end subroutine Prueba
From Java
package 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
0 Kudos
Sikha_r_
Beginner
1,729 Views

hello sir.

i want tocall fortan function in java..bt 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);
        System.out.println(result);
    }
    
}

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'.

0 Kudos
Reply