-1

The following method starts the cmd in Windows and it takes a parameter of the command which need to be run.

I have tested this method using the following commands: net users and it worked fine and it printed the users accounts. but if I run the dir command I get the following error:

java.io.IOEXception:
Cannot run program "dir": CreateProcess error=2, The system cannot find the file specified (in java.lang.ProcessBuilder)

Code :

private String commandOutPut;

        public void startCommandLine(String s) throws IOException{
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(s); // you might need the full path
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String commandOutPut;

        while ((commandOutPut = br.readLine()) != null) {
            this.commandOutPut = this.commandOutPut + "\n" + commandOutPut;
        }
        System.out.println(this.commandOutPut);
    }
atish shimpi
  • 4,461
  • 1
  • 28
  • 46
james
  • 101
  • 1
  • 2
  • 5

4 Answers4

2

Well, obviously, your method does not start cmd. How did you get this notion?

The net command is a standalone command so it runs just fine, but the dir command is not standalone, it is an internal command of cmd.exe, so you cannot run it without launching cmd.exe to execute it.

To get it to work you will have to pass not dir but cmd.exe /c dir or something like that.

Mike Nakis
  • 46,450
  • 8
  • 79
  • 117
0

Don't know if this perception can help you. But, seems that "net users" are recognized as Windows command, since "Execute" dialog can run it. But, for some reason, the "dir" command aren't. When try to run, Windows responds that command was not found.

Additionaly, I tried run Command with inline arguments too, but the arguments are simply ignored. (sorry for bad english)

0

My best guess is that this is because "net" is a real executable (there is a file WINDIR\System32\net.exe"), while "dir" is a builtin command of the command interpreter - it has no executable and is directly executed within cmd.exe.

Howevever you may get around this be invoking "dir" command inside the cmd process. The syntax - as per Microsoft docs - is:

cmd /c dir

There are also some related answers on the site:

How to execute cmd commands via Java

Run cmd commands through java

Community
  • 1
  • 1
Martin Modrák
  • 696
  • 8
  • 17
0

You can use the following code for this

import java.io.*;

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

        }
        catch(Exception e) {} 
        System.out.println("Done"); 
    } 
}
Krutik Jayswal
  • 3,115
  • 1
  • 13
  • 36