Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Running dos commands from Java

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
kibkid
Asked:
kibkid
  • 5
  • 4
1 Solution
 
objectsCommented:
try:

r.exec("cmd /c dir");
0
 
kibkidAuthor Commented:
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
kibkidAuthor Commented:
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
 
objectsCommented:
grab the code at:

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

it displays the process output to stdout.
0
 
kibkidAuthor Commented:
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
 
objectsCommented:
That code will miss every second line, try more like this:

String line = null;
while(null!=(line = in.readLine()))
{
      System.out.println(line);  
}
0
 
kibkidAuthor Commented:
oh ok, thanx man really appreciate it
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now