0

Writing some Java code for running a text executable file under Linux, I have a problem to print out the output of it. This executable file is actually a nmap -sP and so receives to parameters.

Everytime I call for the compiled class, I only can see the first output line but nothing else.

This is the runFile.java file:

import java.lang.Runtime;
import java.lang.Process;
import java.io.*;
import java.lang.InterruptedException;

public class runFile {

    public static void main (String args[]) throws IOException, InterruptedException {


        Runtime r = Runtime.getRuntime();
        Process p = r.exec("/home/diegoaguilar/Dropbox/Buap/SO/file.exe "+args[0]+args[1]);
        InputStream stream = p.getInputStream();
        BufferedReader reader = new BufferedReader (new InputStreamReader(stream));
        String salida = reader.readLine();

        while (salida != null) {
            System.out.println(salida);
            salida = reader.readLine();
        }
        //p.waitFor();
    }


}

So, this is the content of file.exe:

nmap -sP $segment1-$segment1

No matter what arguments, either valid or not I call runFile with, it's always printed to the console something like the first line:

Starting Nmap 5.21 ( http://nmap.org ) at 2013-08-25 02:09 CDT

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
diegoaguilar
  • 8,179
  • 14
  • 80
  • 129
  • You need to read the Process's InoutStream (which is the output from the process), [for example](http://stackoverflow.com/questions/16093754/how-do-i-execute-a-sequence-of-related-console-commands/16094810#16094810) and [exmple](http://stackoverflow.com/questions/11893469/java-execute-a-program-with-options-like-ls-l/11893495#11893495). One pipes the output of the process directly to standard out, the other uses a thread to read the InputStream so as not to block the current thread – MadProgrammer Aug 25 '13 at 07:48

3 Answers3

1

How about sending the output to a temp file and then reading that file.

Process p = r.exec("/home/diegoaguilar/Dropbox/Buap/SO/file.exe "+args[0]+args[1]+" > temp.output");

Now output will go to temp.output file and you should be able to read it easily.

Lokesh
  • 7,810
  • 6
  • 48
  • 78
  • I did, but where does the temp.output file would go to? It's not in local directory, neither in upper one. – diegoaguilar Aug 25 '13 at 07:41
  • It shld be in directory "/home/diegoaguilar/Dropbox/Buap/SO/" else you can also give complete path for temp file like /home/myname/temp.output. – Lokesh Aug 25 '13 at 07:44
  • I assumed it'd go to current directory but it isn't, I'll try by giving the path. – diegoaguilar Aug 25 '13 at 07:48
1

Read the inputstream and errorstream in a separate thread. Also, wait for the threads to join. This will read all messages until command is executed.

LogStreamReader is my custom thread to read streams.

Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);

LogStreamReader lsr = new LogStreamReader(p.getInputStream(), "INPUT");
Thread thread = new Thread(lsr);
thread.start();
LogStreamReader lsr2 = new LogStreamReader(p.getErrorStream(), "ERROR");
Thread thread2 = new Thread(lsr2);
thread2.start();
thread2.join();
thread.join();
hImAnShU
  • 103
  • 5
0

Try putting p.waitFor(); after Process p = r.exec("/home/diegoaguilar/Dropbox/Buap/SO/file.exe "+args[0]+args[1]);

SSaikia_JtheRocker
  • 5,053
  • 1
  • 22
  • 41