Running a fortran program from java

In this post I write an example about how to launch a fortran executable form a java program passing some arguments and getting back a result.
The chosen example uses code written in fortran to get primes, it is from Sieve of Eratosthenes.

program sieve

  implicit none

  integer :: i_max
  integer :: i
  logical, dimension (:), allocatable :: is_prime

  character(len=10) :: arg
  call getarg(1,arg)
  if(arg .ne. '') then
    read(arg,*) i_max
  else
    i_max=100
  end if

  allocate(is_prime(i_max))

  is_prime = .true.
  is_prime (1) = .false.
  do i = 2, int (sqrt (real (i_max)))
    if (is_prime (i)) is_prime (i * i : i_max : i) = .false.
  end do
  do i = 1, i_max
    if (is_prime (i)) write (*, '(i0)', advance = 'yes') i
  end do

end program sieve

Unlike the original code, the program accepts an argument, it is equal to i_max (row 12) and it is the upper limit in the search for prime numbers.

The java code to launch the fortran program is:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

public class Main {

    private final static Logger log = Logger.getLogger(Main.class);

    public static void main(String[] args) {

	int max = 0;
	if (args.length > 0) {
	    try {
		max = Integer.parseInt(args[0]);
	    } catch (NumberFormatException e) {
		System.err.println("Argument" + " must be an integer");
		System.exit(1);
	    }
	}

	List<Integer> primes = new ArrayList<Integer>();

	try {
	    String line;
	    Process p = Runtime
		    .getRuntime()
		    .exec(new String[] {
			    "[put the path to the fortran executable]",
			    Integer.toString(max) });
	    BufferedReader bri = new BufferedReader(new InputStreamReader(
		    p.getInputStream()));
	    BufferedReader bre = new BufferedReader(new InputStreamReader(
		    p.getErrorStream()));
	    while ((line = bri.readLine()) != null) {
		primes.add(new Integer(line));
	    }
	    bri.close();
	    while ((line = bre.readLine()) != null) {
		System.out.println(line);
	    }
	    bre.close();
	    p.waitFor();

	    Iterator<Integer> it = primes.iterator();
	    while (it.hasNext()) {
		log.debug(it.next());
	    }

	} catch (Exception err) {
	    err.printStackTrace();
	}

    }

}

This code is from Execute an external program.
At the lines 29-33 the fortran executable is called with the statement Runtime.getRuntime().exec() with argument a string array whose first element is the path to the fortran executable and the second one is the highest number to which to look for prime numbers.
The second element is passed to the java program as argument.
If you run the java program with 100 as argument you get the sequence of prime numbers between 2 and 97 on the console.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.