1

I am trying to call Fortran 90 program from java program, My Fortran program is as follows:

 !sum.for
program Numbers_sum

  implicit none
  ! -----------------------------------------------Declare

  REAL :: a,b,sum


  ! -----------------------------------------------Input
  print*,"Enter first number..."
  read*, a
  print*,"Enter second number ..."
  read*, b
  ! -----------------------------------------------Compute
  sum = a+b
  ! -----------------------------------------------Output
  print*,"Sum =", sum
  end

Which is working on Fortran compiler. And finally I got an exe file which will execute and give fortran result. I am trying to call it from my java program, My java program as follows:

import java.io.File;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Sum {
    public static void main(String []args) {
        String filePath = "sum.exe";
    if (new File(filePath).exists()) {
    try {

        ProcessBuilder pb = new ProcessBuilder(filePath);
        pb.redirectError();
        Process p = pb.start();
        InputStream is = p.getInputStream();
        int value = -1;
        while ((value = is.read()) != -1) {
            System.out.print((char) value);
        }

        int exitCode = p.waitFor();

        System.out.println(filePath + " exited with " + exitCode);
    } catch (Exception e) {
        e.printStackTrace();
    }
} else {
    System.err.println(filePath + " does not exist");
}
    }
    }

But it is not working.

I am calling the java from my command prompt as follows: image

But cursor blinking only. It is not working.

I did cntrl+c to escape from the java command prompt . That case I gott like the following: image2

Why it is not working. Please help me. How I could read that exe from my java program correctly. Any help will be appreciated!!

5
  • Is your exe not Numbers_Sum (or similar)? also, why call an exe from Java? Commented Apr 21, 2015 at 11:38
  • @Stultuske I need to execute fortran program from java. So I created fortran executable file and calling it from java file Commented Apr 21, 2015 at 11:43
  • Did you run your fortran exe separately? Commented Apr 21, 2015 at 11:45
  • @Stultuske I did. It is working!! Commented Apr 21, 2015 at 11:47
  • @SashaSalauyou when I try to run it it is not working. But when I try to escape using Control key+C then it is getting partially execute the exe file as you can see from my screen shot. Please help me buddy Commented Apr 21, 2015 at 11:48

1 Answer 1

2

Seems that you need to redirect streams here--so input stream of fortran execution process would be redirected to System.in and output stream to System.out. Just put the following lines:

pb.redirectInput(Redirect.INHERIT); 
pb.redirectOutput(Redirect.INHERIT); 

before pb.start().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.