Solved

Running dos commands from Java

Posted on 2004-08-13
9
371 Views
Last Modified: 2012-06-21
Hi guys can I even do this?
How can I run dos commands from a Java app.
For example I know I can open programs from java:

Runtime r = Runtime.getRuntime();
      Process p = null;
      try{
          p = r.exec("notepad");
      }
      catch(Exception e){};

But how can I run the'dir' command?

thanx guys
0
Comment
Question by:kibkid
  • 5
  • 4
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11798377
try:

r.exec("cmd /c dir");
0
 
LVL 92

Expert Comment

by:objects
ID: 11798380
0
 

Author Comment

by:kibkid
ID: 11798389
thanx man it does seem to work since there is no Exception however how can i see the listing?
How can i see the result of dir?
Coz I don't see them.

Thanx for the examples however I couldn't get them I got an error while attempting to the them
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:kibkid
ID: 11798392
I did this:

cmd /c dir > test.txt

and it redirected the results to the file but is there a way to see it on the run screen?
thanx
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 11798401
grab the code at:

http://www.objects.com.au/java/examples/util/ConsoleExec.do

it displays the process output to stdout.
0
 

Author Comment

by:kibkid
ID: 11798448
Hey man thanx a lot but you are way ahead of me in Java knowledge so your code wasn't really easy to get but thanx for everything now I got it to work with your help, this is what I did:


Runtime r = Runtime.getRuntime();
      Process p = null;
      try{
          p = r.exec("cmd /c dir");
          BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
          while(!(in.readLine()).equals(null)){
          System.out.println(in.readLine());   }
      }
      catch(Exception e){
      System.out.println(e);};


0
 
LVL 92

Expert Comment

by:objects
ID: 11798460
That code will miss every second line, try more like this:

String line = null;
while(null!=(line = in.readLine()))
{
      System.out.println(line);  
}
0
 

Author Comment

by:kibkid
ID: 11798544
oh ok, thanx man really appreciate it
0
 
LVL 92

Expert Comment

by:objects
ID: 11798563
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap bin 2 40
Configure a Bean in an XML file 4 42
Running JavaFX on the Raspberry Pi 27 39
passing enum to a method 3 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question