java调用fortran so,从Java调用fortran90 exe程序未执行

博主尝试从Java程序中调用一个Fortran编译的exe文件,但遇到问题。Java代码能够找到并尝试执行exe文件,然而程序没有响应。解决方案指出需要将Fortran程序的输入和输出流重定向到Java的System.in和System.out。通过添加`pb.redirectInput(Redirect.INHERIT);`和`pb.redirectOutput(Redirect.INHERIT);`,可以解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

4Kche.png

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:

vb2Hn.png

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

解决方案

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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值