Can anyone suggest me that how to trigger the commands like cd,ls by using java with code.
Asked
Active
Viewed 218 times
-3
-
With a little effort you would have found something like this: http://stackoverflow.com/questions/8496494/running-command-line-in-java – Eric Sep 09 '14 at 12:06
-
Note: running 'cd' from Java will have no effect, as the command is executed in a new process. – Jonathon Reinhart Sep 09 '14 at 12:07
-
There should be a _Let me Google that for you_ button beside the _add a comment_ button. – Alexis Leclerc Sep 09 '14 at 12:11
1 Answers
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);
}
}

erdem karayer
- 37
- 3