-3

Can anyone suggest me that how to trigger the commands like cd,ls by using java with code.

mahi
  • 191
  • 1
  • 2
  • 16

1 Answers1

0

this is a simple sceranio like this. i run my program in D: and i want to go to a folder in C:\Users\erdemk\Desktop\directory and run a dir command on it. you can use this code:

public static void main(String[] args) throws IOException {
  ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "c: && cd \"C:\\Users\\erdemk\\Desktop\\directory\" && dir");
  builder.redirectErrorStream(true);
  Process p = builder.start();
    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while (true) {
        line = r.readLine();
        if (line == null) { break; }
        System.out.println(line);
    }
}