-3

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

3
  • With a little effort you would have found something like this: stackoverflow.com/questions/8496494/… Commented Sep 9, 2014 at 12:06
  • Note: running 'cd' from Java will have no effect, as the command is executed in a new process. Commented Sep 9, 2014 at 12:07
  • There should be a Let me Google that for you button beside the add a comment button. Commented Sep 9, 2014 at 12:11

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.