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!

1 Answer 1

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}\""
Sign up to request clarification or add additional context in comments.

6 Comments

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
@ManishMankar what exact Java code are you using to pass the parameters to the java Runtime.exec() command?
String commandToBeExecuted="git --git-dir D:\code_coverage\.git blame 'src/RunCodeCoverage.java' | grep e4ecfbe | awk "{print $7}"'"; Process p = Runtime.getRuntime().exec(commandToBeExecuted);
@ManishMankar I have amended the answer accordingly
Thanks@VonC! but still am getting issue "fatal: cannot stat path ''src/RunCodeCoverage.java'': No such file or directory "
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.