3

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?

2 Answers 2

3

You want to use the $() syntax.

eg

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

2
  • 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? Commented Jun 17, 2016 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 $(...)" Commented Jun 17, 2016 at 22:19
3

Use the exec command of find:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .