Suresh Kumar
Suresh Kumar

Reputation: 95

Run fortran exe in java

Respected sir/madam,

I have a fortran exe which takes a input file and produces output file by doing some manipulation on input file.I am able to run the command in linux terminal.(I think fortran compiler is availble in linux).Now please suggest how to run this fortran executable file using java(in Linux machine).

What i attempted is,

 String cmd="fortranExe arg1 arg2";
 //fortranExe=exe path
 //arg1,arg2 are arguments to fortran executable program
 Process p=Runtime.getRuntime().exec(cmd); 

But i am not getting output.When i tried to run Linux commands such as ls,dir are giving output.Is anything required for running fortran code in java?

Upvotes: 1

Views: 1724

Answers (3)

Naytzyrhc
Naytzyrhc

Reputation: 2317

No, if it's a reqular binary for the platform you are running your JVM on, it shouldn't matter.

How are you running the binary, when you run from console?

Once the process was generated successfully, you can read its stdout like this:

BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = br.readLine()) != null)
{
    System.out.println(line);
}
br.close();

If there was any runtime problem executing the process, e.g. no permission to run the binary etc., the process.exitValue() will return 127.

EDIT: seeing the other comments, I can see that you are using redirected in/output to your binary.

So in fact there are no parameters, but you need to open InputFileName.txt and use the process.getOutputStream() Object to write to your process. No need to set OutputFilename.txt, because you read the output from the InputStream and if necessary can write it yourself to a file.

This answer explains it in detail:

https://stackoverflow.com/a/3644288/435583

https://stackoverflow.com/a/6796053/435583

Cheers,

Upvotes: 1

Santosh
Santosh

Reputation: 17923

Apart from using Runtime, a more cleaner way will be to use JNA. See specific example for Fortran here.

You can even call individual pieces like subroutines of your Fortan program directly from Java. JNA is generally used to invoke C/C++ programs and are generally suited for this type of use cases and I guess yours use case fits well here.

Upvotes: 0

Austin
Austin

Reputation: 4929

Try using something like this

Process process = new ProcessBuilder("C:\\PathToExe\\fortran.exe","param1","param2").start();

Got this from

Java Programming: call an exe from Java and passing parameters

More info on ProcessBuilder: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

Upvotes: 1

Related Questions