Solved

Running dos commands from Java

Posted on 2004-08-13
9
366 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now