Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Cygwin bash using ProcessBuilder

843810Jan 2 2009
Hi,

i am trying to execute a command in a cygwin-bash.

the java code is:
import java.io.*;

public class cmd {
	public static void execute(String cmd_array[]) {
		execute(cmd_array, null);
	}
	public static void execute(String cmd_array[], String work_directory) {
		try {
			ProcessBuilder pb = new ProcessBuilder(cmd_array);
			if(work_directory != null) {
				pb.directory(new File(work_directory));
			}
			Process process = pb.start();

			String line;

			System.out.println("Here is the standard output of the command:\n");
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = stdInput.readLine()) != null) {
				System.out.println(line);
			}

			System.out.println("Here is the error output of the command:\n");
			BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			while ((line = stdError.readLine()) != null) {
				System.out.println(line);
			}
			process.waitFor();
			System.out.println(process.exitValue());
		} catch(IOException e) {
			e.printStackTrace();
		} catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}
the following is working:
String[] cmd_array = new String[2];
cmd_array[0] = "C:/Program Files/System/Cygwin/bin/bash.exe";
cmd_array[1] = "--version";
cmd.execute(cmd_array,"C:/Program Files/System/Cygwin/bin");
this does not work:
String[] cmd_array = new String[3];
cmd_array[0] = "C:/Program Files/System/Cygwin/bin/bash.exe";
cmd_array[2] = "--login";
cmd_array[2] = "-c 'ls'";
cmd.execute(cmd_array,"C:/Program Files/System/Cygwin/bin");
i get the following output:
Here is the standard output of the command:

Here is the error output of the command:

/usr/bin/bash: - : invalid option
Usage:  /usr/bin/bash [GNU long option] [option] ...
        /usr/bin/bash [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option
It seems as if the argument --login is not passed correctly.

Thanks for any help.
Robert
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2009
Added on Jan 2 2009
0 comments
467 views