Fortran90 exe ​​program call from java fails

I am trying to call a Fortran 90 program from a java program. My Fortran program looks like this:

  !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 

What works with the Fortran compiler. And finally, I got an exe file that will execute and give the result of fortran. I am trying to call it from my java program. My java program looks like this:

 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 does not work.

I call java from the command line as follows: image

But the cursor only blinks. He does not work.

I did cntrl+c to exit the java command line. In this case, I would like to: image2

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

+1
source share
1 answer

It seems that you need to redirect the threads here, so the fortran runtime input will be redirected to System.in and the output stream to System.out . Just put the following lines:

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

to pb.start() .

+1
source

Source: https://habr.com/ru/post/987032/


All Articles