?
Solved

Running dos commands from Java

Posted on 2004-08-13
9
Medium Priority
?
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 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