wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with runtime.exe and javac from command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with runtime.exe and javac from command line" Watch "Help with runtime.exe and javac from command line" New topic
Author

Help with runtime.exe and javac from command line

CSLA Sanchez
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Dear Developers:
Can you tell me if I am doing anything wrong with the following code? I am trying to get DIR results using runtime.exec. I am using JBuilder 9:
System.out.println("Hello World!");
Runtime runtime = Runtime.getRuntime();
try{
Process proc = runtime.exec("dir"); // like the dir command at DOS prompt
System.out.println(proc);
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
}catch(IOException e){}
System.out.println("Good BYE");
With the above I get results for the first and last System.out.println, but nothing in the middle. i would like to see what are the dir contents of the directory. Any hints?
Also, I tried to run the program from the commad line, and I get the .java file to compile using javac, but when I try to execute the class using "java" this is what I get:
"Exception in thread "main" java.lang.NoClassDefFoundError" and something else.
Any ideas?
Thanks in advance!
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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 ]
Cecil Phillip
Ranch Hand

Joined: Nov 05, 2001
Posts: 40
Here's the output I get when I run the program

C:\Code\Java>java Class
Hello World!
CreateProcess: dir error=2
java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Class.main(Class.java:9)
Good BYE
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The error that Cecil received indicates that the JVM could not find the command you are asking to be executed (i.e. "dir"). After taking a closer look at my output, I realized that the dir command that was running on my system was a Cygwin alias for an ls (cygwin is a unix emulator that runs on windows). So since my Cygwin install directory is in my system path, the Java class I ran was finding cygwin�s dir command and executing it. When I removed the Cygwin directory from my path, I get the same IOException.
So this tells us that the java class is not finding the dir command. I suspect the reason why is as follows: If I am not mistaken (and someone else can correct me if I am) unlike previous versions of Windows, Windows XP is not built on top of MD-DOS. So MS-DOS (and all its commands) does not exist on a Windows XP System natively. There is a cmd.exe program (<windows-root>/System32/cmd.exe) which emulates a DOS session and provides basic DOS commands such as dir, but those commands are encapsulated within the cmd.exe program and are therefore not natively available. So there is no dir.exe or dir.bat or dir.com (or what ever file type it was back in the day) for the JVM to find and use.
Ultimately, the easiest thing to do, if your goal is to get a directory listing, is to use Java�s native classes (see the examples and tutorials I posted previously). If your goal is to experiment with the Runtime.exec() method, try executing a different command. Try writing a simple bat file that echos something back and execute it. Or use some other basic Windows executable. For example, try Process proc = runtime.exec("hostname") {which should execute C:\Windows\System32\hostname.exe} and will display your system's hostname. This assumes that System 32 is in your path (which it should be on any Windows System). If you still get an IOException, fully qualify the command with: Process proc = runtime.exec("C:/Windows/System32/hostname.exe"); {Notice the use of forward slashes rather than back slashes, other wise you must escape the backslashes like this: C:\\Windows\\System32\\hostname} Even fully qualifying the command assumes that Windows is installed in C:\Windows. � As you can see, with the runtime.exec() method, we have to make a lot of assumptions about system paths, install directories, etc. Hence why this is a method you would want to leverage with extreme care (and with lots of error handling) in a "real-world" production application.
Hope that helps...
Regards,
Mark
Donny Nadolny
Ranch Hand

Joined: Mar 05, 2003
Posts: 32
Just to add to that, If your intention is to get a directory listing, there's a way using pure java to do it. Using a File object (passing the current directory) you can get a listing of all files and folders in the specified directory:

However, if your intention was to experiment with Runtime.exec() then follow Mark Vender's idea.


- Donny Nadolny<br />The pen is mightier than the sword, and considerably easier to write with.
CSLA Sanchez
Greenhorn

Joined: Feb 07, 2004
Posts: 5
Thank you all for your help. Based on your advice, the following subtitution does what I want to do:
Process proc = runtime.exec("cmd /c dir");
And it works like a charm! I will try the other solution which involves Java native options.
THANKS!!
--Cecilia.
sri san
Greenhorn

Joined: Jul 30, 2008
Posts: 18
Hey Cecilia! Thanks much for your post, helped me too!

--Sannidhi--
sri san
Greenhorn

Joined: Jul 30, 2008
Posts: 18
Hey Mark Vedder! Thanks for sharing your inputs, helped me too!

--Sannidhi--
 
 
subject: Help with runtime.exe and javac from command line