I wish to compile the results of a find command where the find command returns the relative filepath of a java file located in subdirectories. E.G., I am in ./ and I want to run javac on a file, someFile.java, but I don't know at "command-type-time" what the relative path is.

Running find . -name someFile.java returns the correct relative path (and only the correct one so long as someFile.java is uniquely named within subdirectories of .) I wish to compile THIS file. So I have attempted javac | find . -name someFile.java but I am not sure about why this is not working. Is this even possible?


You want to use the $() syntax.


javac $(find ./someDir/anotherDir/ -name someFile.java)

  • this worked. i wonder, is it possible to invoke this command using Java's ProcessBuilder? It takes commands as an array of strings and it seems that $(...) is not properly represented as one of those strings in the array. I.E., it seems ProcessBuilder fundamentally cannot handle composite commands such as this? – Alexander Bollbach Jun 17 '16 at 20:39
  • That's a bash command; you'd need to run an equivalent of bash -c "javac $(....)" as the command to run in this case; the array would be "bash", "-c", "javac $(...)" – Stephen Harris Jun 17 '16 at 22:19

Use the exec command of find:

find . -name someFile.java -exec javac {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.