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.

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:

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.

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.