Can Java program evoke and run a FORTRAN program directly?

Can Java program evoke and run a FORTRAN program directly?

Post by Shaw » Fri, 08 Dec 2006 02:41:00


Hi,

I have a Java program. In order to use an existing FORTRAN program for
doing some computation, I hope I can evoke and run the FORTRAN program
from my Java program.

Somebody has told me that Java has to call a C program, which calls the
FORTRAN program. I am wondering if there is some better ways to do it.

Thank you very much.
 
 
 

Can Java program evoke and run a FORTRAN program directly?

Post by Matt Humph » Fri, 08 Dec 2006 02:48:34


Java can invoke the OS which will in turn launch the FORTRAN program. You
can connect to the input, output and error streams of the program also, if
you want. Look for Runtime.exec () and a new helper called ProcessBuilder.
There are some gotchas, but you can find sample code, etc by searching the
web or this newsgroup.

Matt Humphrey XXXX@XXXXX.COM http://www.yqcomputer.com/

 
 
 

Can Java program evoke and run a FORTRAN program directly?

Post by Shaw » Fri, 08 Dec 2006 04:08:47


Thank you. I have got it running, shown below. I will improve it by
using ProcessBuilder later.

<JAVA>
public class DoRuntime {
public static void main(String args[]) throws IOException {

System.out.println("========JAVA program is going to run a FORTRAN
program========");

String cmd = "myscript"; //it contains several commands,
including one start FORTRAN
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(cmd);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", cmd);

while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("=======JAVA program has finished running a
FORTRAN program=========");

}
}
</JAVA>