2

I need help to read a decimal number in a text file using JNA. It looks funny but the problem is real.

I created a simple Java program which calls a simple Fortran subroutine using JNA.

Main class :

package Appli;

public class Appli {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // call to subroutine lecture with LECT 
        LectInterface.LECT.lecture();

    }
}

Interface :

package Appli;

import com.sun.jna.Library;
import com.sun.jna.Native;

/**
 * Interface.
 */
public interface LectInterface extends Library {

    // dynamic library.
    public final static LectInterface LECT = (LectInterface) Native.loadLibrary("libLect.so", LectInterface.class);

    /**
     * Fonction LECTURE.
     *
     */
    public double lecture();
}

The Fortran subroutine just reads a number in a text file:

SUBROUTINE LECTURE ()
      DOUBLE PRECISION VALDOUBLE

      INTEGER IFILE
      PARAMETER(IFILE=1)

      WRITE(*,*)'START LECTURE'

      OPEN(UNIT=1,FILE='/home/Fortran/fort.txt')
      READ(IFILE,*) VALDOUBLE
      WRITE(*,*)VALDOUBLE

      WRITE(*,*)'END LECTURE'
END SUBROUTINE

The text file :

2.36

When I call this subroutine with my Java program (using NetBeans) it returns :

run :
 START LECTURE
     2.000000000000000
 END LECTURE

I use NetBeans 8, java 7, gfortran and JNA-4.1.0

I compile with:

gfortran -fno-underscoring -fPIC -c -g -o LECTURE.o LECTURE.f

Then

gfortran -shared -o libLect.so LECTURE.o

I can't explain why the READ function only takes the first part of the number (before the decimal point), for example:

2.200          =>  2.000000000000000
0.12           =>  0.000000000000000
12.023         =>  12.000000000000000
100000.0182    =>  100000.000000000000000

I did several tests with the compilers ifort (Intel) and pgf90 (PGI).

With pgf90, the result is still false, but with ifort, the value is read correctly.

Compilation: ifort -c -fPIC LECTURE.f

Creation of the dynamic library: ifort -shared -o libLect.so LECTURE.o

Result: 2.3

I also compile the sources with gfortran using the same options than ifort. The value is false (2.0).

Is there a compilation option to add with gfortran?

stam08
  • 21
  • 2
  • What does the Java side look like? – cup Aug 01 '14 at 12:54
  • Possible [duplicate](http://stackoverflow.com/q/1126028/2970947). – Elliott Frisch Aug 01 '14 at 13:12
  • My Subroutine run normaly in stand alone (.exe). But not with JNA even if i use format. – stam08 Aug 01 '14 at 13:31
  • It looks like your numbers are being converted to integer by the time they get output. If you look at @ElliottFrisch's reference, you need to explicitly parse as a float, where it looks like you're parsing as an integer. Your different compilers may have different defaults. – technomage Aug 01 '14 at 16:11
  • 1
    I disagree that this is a duplicate. The fortran code is perfectly fine and something is going strangely wrong due to JNA (which I'm not familiar with). You can try an explicit format `read(1,'(f14.7)')valdouble` but it should not be necessary. ps. as another shot in the dark i'd try a unit number > 10 .. – agentp Aug 01 '14 at 16:19
  • thanks for you answer. @technomage I try different format for the read and the write function, but I have the same problem. – stam08 Aug 04 '14 at 07:15
  • @george I try unit number > 10, same issue. I will try different options of the compilers, maybe that could works. – stam08 Aug 04 '14 at 07:26
  • I have encountered the same problem with version 4.8.2 of gfortran. This is why I have found your question. I have created a minimal example that you can find here https://github.com/Gjacquenot/DemoJavaFortranInterface . With gfortran 6.2, I no longer have the problem – Guillaume Jacquenot Dec 10 '16 at 18:54

0 Answers0