0

I have a Fortran exe. What I need to do.... I need to call that exe through java in Linux. After that it should ask for input file and output file. This is my code:

Process process = new ProcessBuilder("/home/admin/Documents/file.out",
                                     "input","output").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

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

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

It is running but nit asking for input and output file

01101988
  • 5
  • 3
  • See [here](http://stackoverflow.com/questions/9053600/java-on-linux-start-a-process-using-runtime-exec-processbuilder-start-with) – Jens May 26 '14 at 10:05
  • Ask what / who for the files? – Stephen C May 26 '14 at 10:10
  • Process process = new ProcessBuilder("/home/neha/Documents/a.out","input","output").start(); This line is working properly,it wil generate some text file.but the thing is that i need to generate input file and output file – 01101988 May 26 '14 at 10:13
  • That doesn't answer my question. Nothing is "asking" anything in that line. – Stephen C May 26 '14 at 10:14

2 Answers2

0

To call external programms in java you need the java.lang.Runtime package. If you want a more convenient API have a look at Apache Commons Exec.

BetaRide
  • 16,207
  • 29
  • 99
  • 177
0

None of the code you have shown us "asks" anything.

I can see where you are passing two names (are they file names?) to your fortran program. But then it would be up to the fortran program to open those files and do something with them. If that's not happening, then the problem is in the fortran code ...

On the other hand, if your intention is to open the files in the Java code, and pass the file handles to the fortran program (as its standard input and standard output), then your code doesn't attempt to do that. You need to read the javadocs for ProcessBuilder. Pay attention to the stuff about redirecting input and output for the child process.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • sry for confusion.Actualy the thing is i m caling that fortan exe file.after that whatever output i vl get,i need to store in a output file.for output file i need to set path and filename – 01101988 May 26 '14 at 11:24
  • My answer explains two ways to do that. Either 1) pass the filenames to the fortran program and change it to open the files itself, or 2) open the files in Java and call the fortran program with the opened files as its standard input and standard output. – Stephen C May 26 '14 at 22:20
  • hello sir,after fortan exe call in java.it is generating some text file in my project folder.so tell me how i vl set the output file path and filename and also i want to display the output file in desktop – 01101988 May 28 '14 at 05:57
  • 1) What is generating the text file? The Fortran or the Java? Show me the relevant code. 2) To put the file on the desktop, you need to create the file with a pathname that is in the desktop directory. If you are using Linux, that is probably "/home/neha/Desktop" – Stephen C May 28 '14 at 07:44
  • private void fortanButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try { Process process = new ProcessBuilder("/home/neha/Documents/a.out").start(); } catch (IOException e) {} } – 01101988 May 28 '14 at 08:14
  • a.out is fortan exe file,which is generating temp.txt file.i want to show this text file in desktop after callin this fortan exe – 01101988 May 28 '14 at 08:16
  • String filepath="temp.txt"; Desktop dt=Desktop.getDesktop(); try { dt.open(new File(filepath)); }catch(Exception e){ – 01101988 May 28 '14 at 08:20
  • wl this work in linux – 01101988 May 28 '14 at 08:20
  • Show us the code in the Fortran file that creates the "temp.txt" file. That is the code that you need to change. Alternatively, you could get the Java code to >>rename<< the file ... after the Fortran has finished. – Stephen C May 28 '14 at 09:05
  • Process process = new ProcessBuilder("/home/neha/Documents/a.out").start() ; this code is not working for caling fortan exe file.it showing error like this java.io.IOException: Cannot run program "/home/neha/Documents/a.out": error=13, Permission denied – 01101988 May 28 '14 at 09:06
  • Well check the file and directory permissions then! – Stephen C May 28 '14 at 09:08
  • nw this code is not runing – 01101988 May 28 '14 at 09:09
  • i m sending fortan code – 01101988 May 28 '14 at 09:10
  • program main character(len=10) :: name name = 'iisc' !read(*,*) name open(unit=1,file="new.txt",status="replace") write(1,*) name end program main – 01101988 May 28 '14 at 09:10
  • Change "new.txt" to the pathname of the file that you *want* it to write to. Seriously dude, this is all really, really basic stuff. – Stephen C May 28 '14 at 09:11