wood burning stoves
The moose likes Beginning Java and the fly likes exec() and my stupidity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exec() and my stupidity" Watch "exec() and my stupidity" New topic

exec() and my stupidity

Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
I just wanted to share my stupidity and how I used up all the disk space, about 90 GB with a couple of lines of code.

All I wanted to do was to list out all the files in a directory (including the sub-directory) containing a set of string and save the information in a file. I thought find would help, I tested that as well....

find . -exec grep -i "TEXT_TO_FIND" '{}' \; -print >TEXT_TO_FIND.txt

However, I need to pick up the string to search dynamically and build the command string dynamically, after figuring out the right way of building the command string , I ran my program. I thought exec() would wait
for a signal from the Operating system that the task has finished. I think it does not. Even if it did, my program will never complete because it would also search in the files which I have been creating by redirecting,
the output and thus grep was running indefinitely until all the disk space was utilized.

I just want to confirm that Runtime.getRuntime().exec(cmd); does not wait for any signal from OS.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
It sure does. Be sure to read and follow all the advice in this article.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
Moving thread as Runtime.exec() is too difficult for “beginning”
By the way: the ProcessBuilder class makes Runtime.exec() slightly easier to use. But only slightly. You still need to “drain” both Streams.
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8942

Raghu Devatha wrote:I just want to confirm that Runtime.getRuntime().exec(cmd); does not wait for any signal from OS.

Seems to me that you've leapt to an implementation rather too quickly. I'm pretty sure the File (java.io.File) class, along with Matcher (java.util.regex.Matcher), has everything you need to to do what you want; and it's likely to be far more dynamic than a Runtime.exec()-based solution.

If you're writing a script, find's your man (although it only exists on Unix/Linux; unless you're into Cygwin installations). If you're writing Java code, use the classes it provides.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link: http://aspose.com/file-tools
subject: exec() and my stupidity
It's not a secret anymore!