Your program works fine for me, and shows all the items in my directory. It�s possible you are getting an Exception thrown, but are not aware of it since your catch block doesn�t do anything. Add something to your catch block to see if you are getting an IOException:
Catching and then not handling an exception is one of the
worst things you can do in a program; it makes debugging a nightmare. At the
very least throw in a output statement like the one I did. The e.printStackTrace(); will print a stack trace to standard error. The first line of the Stack Trace typically includes the Message that the getMessage() method prints. I personally just like to print the message to standard out and then the stack trave to standard error - the reasons why have more to do with the way I have my development environment set up. Ultmiately however, in a final program, you would want to handle the exception by doing something.
With that in place you might see what is going wrong. If not, let us know and we can look at what else might be amiss.
Also, if this is just an exercise to play with and learn about the runtime.exec() method, then no biggie.
But it should be noted that using the runtime.exec method breaks java�s Write Once, Run Anywhere motif. It locks your application into a specific platform. For example you could not run your program on a Mac, Unix, Linux, or PDA system. Using runtime.exec() can also cause you to break good Object Oriented Design. If you are trying to learn a way to view a directory's contents, there are
java based ways of doing such. Take a look at
example 31 - Listing the Files or Subdirectories in a Directory at JavaAlmanac.com for an example. Also look at
Sun�s online Tutorial in the I/O lesson under the Essential Java Classes trail.
As for the java.lang.NoClassDefFoundError you are getting � that indicates that the java virtual machine cannot find the class you are trying to load. It�s potentially a classpath issue. Look at the "Your Fist Cup of Java" information at the Sun Tutorial and the
How to Set the Classpath from JavaRanch's FAQs. Lastly, take a look at
this "NoClassDefFoundError" thread for some more discussion on the subject. If none of that helps, post the exact command you are using and the full error message you get and someone can help you.
Regards,
Mark
[ February 08, 2004: Message edited by: Mark Vender ]