Question

I would like Apache Commons-exec to run:

git status | head -n1 | cut -c13-

However seems it can not execute the command line and give error, Any ideas?

CommandLine cmdLine = CommandLine.parse( "git status | head -n1 | cut -c13-" );
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory( file );
executor.execute( cmdLine );

Error:

error: unknown switch `n'
usage: git status [options] [--] <filepattern>...
Was it helpful?

Solution

CommandLine.parse won't create a bash shell to interpret your pipe.

As described in "How to pipe a string argument to an executable launched with Apache Commons Exec?":

You cannot add a pipe argument (|) because the [here, in your case] 'git status' command won't accept that.
It's the shell (e.g. bash) that interprets the pipe and does special processing when you type that commandline into the shell.

You should use a ByteArrayInputStream to feed the outuput of one command to another;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top