-2

OK, here we go: When I pass arguments through the static main, my program works fine. but when I pass the same "arguments" declaring inside main, doesnt works. If sounds confused, here's the code. All I want is to do these thing works with no args through main. Here's the DEBUG: https://i.stack.imgur.com/sbGyo.png

import java.io.*;
import java.util.*;

public class DoProcessBuilder extends Thread {
  public static void main (String[] args) throws IOException, InterruptedException {
    DoProcessBuilder teste = new DoProcessBuilder();

    String[] uia = {"ls","-al","|","grep","bash"};
    teste.ExecCommand(uia);    // here this not works, WHY? if I execute the "java DoProcessBuilder ls -al | grep bash" works fine?

    teste.ExecCommand(args); // works fine!

  }

  public  String ExecCommand(String args[]) throws IOException, InterruptedException {
    StringBuffer x = new StringBuffer();
    if (args.length <= 0) {
        System.err.println("Need command to run");
    }

    Process process = new ProcessBuilder(args).start();
    process.waitFor();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = "";

    while((line = br.readLine()) != null){
        x.append(line+"\n");
    }
    System.out.println("\nCOMMAND OUT \n"+x.toString());

    return x.toString();

  }
}
Sycon
  • 1
  • 2
  • 5
    please show us your stacktrace "not works" is not helping us – Philipp Sander Jan 30 '14 at 13:09
  • 2
    It should work. Like Philipp said, please post the stack trace. – Someone Jan 30 '14 at 13:09
  • 1
    print both array and check if they are equal – Philipp Sander Jan 30 '14 at 13:10
  • 2
    With the given information, the only thing I can say is that `uia` and `args` don't contain exactly the same strings. Use breakpoints to verify this. – Theodoros Chatzigiannakis Jan 30 '14 at 13:11
  • IS this an exercise or do you really want to use this? Using external commands is not the java way of doing things! There is almost NEVER an urge to invoke external commands. For the given task, you may use File#listFiles() and filter on the received list of files. – Gyro Gearless Jan 30 '14 at 13:21
  • Gyro, I dont want to list files, this is only an example. I want to control the OS through the shell. I debugged this in eclipse and checked. both args and uia vars are equal. – Sycon Jan 30 '14 at 13:26

2 Answers2

2

You are trying to execute a shell pipe with ProcessBuilder. That won't work. The pipe | is no valid argument for the ls command. You must start a shell new ProcessBUilder("sh").

And it works fine with args because your program do not get the pipe and the rest from the calling shell. Your program only gets ls -al.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • execute this programm with args ls -al | ... It works – Sycon Jan 30 '14 at 13:23
  • 1
    If you execute `java DoProcessBuilder ls -al | grep bash` from a shell you never will get `|` `grep` `bash` into your program. – PeterMmm Jan 30 '14 at 13:33
  • Peter, did you tryied to execute this programm? compile these programm, exatcly as it is. go to your command/shell and execute java DoProcessBuilder ls -al | grep something . it works! – Sycon Jan 30 '14 at 13:46
  • Of course you get some output, but this output is the list of files getting from your program (ls -al) and then your program stdout pipes into grep from the shell, where you issues the command in the example. Your program is not calling `grep` at all. Do a `System.out.println(Arrays.toString(args))` and you see. Over. – PeterMmm Jan 30 '14 at 13:55
  • 1
    You're right Peter . and I should do the ProcessBuilder as you mention. TNX! – Sycon Jan 30 '14 at 13:58
0

There is no issue, the place you are mention. But with teste.ExecCommand(args); you will get ArrayIndexOutOfBoundsException

   StringBuffer x = new StringBuffer();
    if (args.length <= 0) {
        System.err.println("Need command to run");
    }

    Process process = new ProcessBuilder(args).start(); // ArrayIndexOutOfBoundsException
    process.waitFor();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = "";

    while((line = br.readLine()) != null){
        x.append(line+"\n");
    }
    System.out.println("\nCOMMAND OUT \n"+x.toString());

    return x.toString();
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • I know , if you execute with no args, it does that. Executes this with args like I mention. – Sycon Jan 30 '14 at 13:27