Can anyone suggest me that how to trigger the commands like cd,ls by using java with code.
-
With a little effort you would have found something like this: stackoverflow.com/questions/8496494/…Eric– Eric2014-09-09 12:06:27 +00:00Commented Sep 9, 2014 at 12:06
-
Note: running 'cd' from Java will have no effect, as the command is executed in a new process.Jonathon Reinhart– Jonathon Reinhart2014-09-09 12:07:37 +00:00Commented Sep 9, 2014 at 12:07
-
There should be a Let me Google that for you button beside the add a comment button.Alexis Leclerc– Alexis Leclerc2014-09-09 12:11:54 +00:00Commented Sep 9, 2014 at 12:11
Add a comment
|
1 Answer
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);
}
}