12

Possible Duplicate:
Get output from a process
Executing DOS commands from Java

I am trying to run a cmd command from within a JAVA console program e.g.:

ver

and then return the output of the command into a string in JAVA e.g. output:

string result = "Windows NT 5.1"
Community
  • 1
  • 1
Mike
  • 2,266
  • 10
  • 29
  • 37
  • 3
    You should have searched this before you ask this question. When I search for [run Windows commands in JAVA](http://stackoverflow.com/search?q=run%20Windows%20commands%20in%20JAVA) in search of stackoverflow, I got below results... http://stackoverflow.com/questions/2935326/java-library-api-to-help-run-windows-commands http://stackoverflow.com/questions/4031390/executing-dos-commands-from-java http://stackoverflow.com/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings – Fahim Parkar Jan 22 '12 at 18:28
  • This Link will help you: https://www.codepuran.com/java/execute-dos-command-java/ – Akshay Pethani May 26 '17 at 13:45

4 Answers4

31

You can use the following code for this

import java.io.*; 

    public class doscmd 
    { 
        public static void main(String args[]) 
        { 
            try 
            { 
                Process p=Runtime.getRuntime().exec("cmd /c dir"); 
                p.waitFor(); 
                BufferedReader reader=new BufferedReader(
                    new InputStreamReader(p.getInputStream())
                ); 
                String line; 
                while((line = reader.readLine()) != null) 
                { 
                    System.out.println(line);
                } 

            }
            catch(IOException e1) {e1.printStackTrace();} 
            catch(InterruptedException e2) {e2.printStackTrace();} 

            System.out.println("Done"); 
        } 
    }
Community
  • 1
  • 1
Kamran Ali
  • 5,904
  • 2
  • 26
  • 36
5

You can use Runtime exec in java to execute dos commands from java code.

Based on Senthil's answer here:

Process p = Runtime.getRuntime().exec("cmd /C ver");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()),8*1024);

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

// read the output from the command

String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) 
System.out.println(s.replace("[","").replace("]",""));

Output = Microsoft Windows Version 6.1.7600

Ryan M
  • 18,333
  • 31
  • 67
  • 74
RanRag
  • 48,359
  • 38
  • 114
  • 167
2

You can do something like:

String line;
Process p = Runtime.getRuntime().exec ("ver");
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error =new BufferedReader(new InputStreamReader(p.getErrorStream()));

System.out.println("OUTPUT");
while ((line = input.readLine()) != null)
  System.out.println(line);
input.close();

System.out.println("ERROR");
while ((line = error.readLine()) != null)
  System.out.println(line);
error.close();

On comment of @RanRag, the main issue is Windows versus Unix/Mac.

  • WINDOWS: exec("cmd /c ver");
  • UNIX FLAVOUR: exec("ver");
havexz
  • 9,550
  • 2
  • 33
  • 29
1

Have a look at java.lang.Runtime or, better yet, java.lang.Process

This might help you get started.

duffymo
  • 305,152
  • 44
  • 369
  • 561