2

I am trying to run below git command using java program on windows.

 git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk '{print $7}'

it gives me error :

fatal: cannot stat path '$7}'': No such file or directory

While running this command from command prompt, it gives me results as desired.

Please help!

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60

1 Answers1

1

On a CMD in Windows, I can make it work using double quotes for the awk parameters:

 git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk "{print $7}"

Note that my awk.exe comes from Gnu On Windows.

The OP mentions using that command in Java with:

String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'"; 
Process p = Runtime.getRuntime().exec(commandToBeExecuted);

But you never pass all parameters as a single string.
Use an array, as in "Using Quotes within getRuntime().exec" and in "How to make pipes work with Runtime.exec()?"

Process p = Runtime.getRuntime().exec(new String[]{"cmd", "/c", commandToBeExecuted);

Escape the double quotes in commandToBeExecuted:

 commandToBeExecuted = "git --git-dir D:\code_coverage\.git blame  'src/RunCodeCoverage.java'
 | grep e4ecfbe | awk \"{print $7}\""
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks VonC, it works through CMD but while running it from java program using java Runtime then it give the mentioned error.: fatal: cannot stat path '$7}'': No such file or directory – Manish Mankar Jul 03 '15 at 07:31
  • @ManishMankar what exact Java code are you using to pass the parameters to the java `Runtime.exec()` command? – VonC Jul 03 '15 at 07:46
  • String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'"; Process p = Runtime.getRuntime().exec(commandToBeExecuted); – Manish Mankar Jul 03 '15 at 09:29
  • @ManishMankar I have amended the answer accordingly – VonC Jul 03 '15 at 10:25
  • Thanks@VonC! but still am getting issue "fatal: cannot stat path ''src/RunCodeCoverage.java'': No such file or directory " – Manish Mankar Jul 03 '15 at 10:47
  • @ManishMankar add the right parent directory as a third parameter of `exec()`(http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java.io.File%29): http://stackoverflow.com/a/6811578/6309 – VonC Jul 03 '15 at 11:08