Search code examples
javaprocessinputstreamprocessbuilder

Reading output from java.lang.Process - There is nothing to read


I'm trying to execute terminal command in linux trough Java and i cant get any input from inputStream.

This is my code

            ProcessBuilder build = new ProcessBuilder("/usr/bin/xterm", "find /home");

    Process pr = null;
            BufferedReader buf;
    try {
            build.redirectErrorStream(true);
            pr = build.start();
    buf = new BufferedReader(new InputStreamReader( pr.getInputStream()));

    String line = buf.readLine();
    pr.waitFor();
    while (true) {
    System.out.println(line + "sadasdas");
            line = buf.readLine();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

Process is executed and immediately terminal closes, and no output is catched and printed. On the other hand if i will compose an unknown command i get all the lines with tips how to use commands. Same problem i had with windows cmd. I was trying to use getRuntime.exec(cmd) method but the end is the same.

I've also tried to created separate threads for process and reader which looks like this

public class kurdee
{
    public static Thread thread;
public kurdee()
{
List cmd = new LinkedList();
    cmd.add(new String("/usr/bin/xterm"));
    cmd.add(new String("find"));
    thisProc thispr = new thisProc(cmd);
    this.thread = new Thread(thispr);
    thread.start();
    reader rd = new reader(thispr.proc);
    Thread thread1 = new Thread(rd);
    thread1.start();}

public static void main(String args[]) 
{

    java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                kurdee kurd = new kurdee();

            }
        });





}
}
class reader implements Runnable
{

    private BufferedReader buf;
    private Process proc;
    public reader(Process proc)
    {
        this.proc=proc;
        this.buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    }

    public void run()
    {
                String line="";
                System.out.println("Thread is alive");
                try{
                    //Thread.sleep(1000);
        line = buf.readLine();
                }catch(Exception ex){System.out.println(ex + " before first while started");}
                while(kurdee.thread.isAlive())
                {
                    System.out.println("Thread is alive");

            while(line!=null)
            {
                                try{
                //System.out.println(proc.exitValue());
                System.out.println(line + " asd");
                line=buf.readLine();
                                }catch(Exception e){System.out.println(e + " Inner while loop");}
            }
                }
    }
}

class thisProc implements Runnable
{
    private ProcessBuilder build;
    public static Process proc=null;

    public thisProc(List<String> args)
    {
        this.build = new ProcessBuilder(args);
        build.redirectErrorStream(true);
                try{

                this.proc = build.start();
                }catch(Exception ex){System.out.println(ex + " proc class");}



    }

    public void run()
    {
         try{

                proc.waitFor();
                }catch(Exception ex){System.out.println(ex + " proc class");}       
    }
}

But with any combination of invoking threads etc i make there is still nothing to read.

I'm trying to use command "find /home -xdev -samefile file" to get all hard links to file so maybe there is an easier way.


Solution

  • First, don't try to execute the command with xterm, that's pointless; just do it directly. Secondly, be careful when you compose your array of command strings to put one word into each string; passing, for example "find /home" as a single string among many to ProcessBuilder is going to error out.