1
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MyCommand {

    public static void main(String[] args) {

        String [] commandList = {"/bin/bash","-c", "/home/atul/Desktop/javaprogramm/" , "mkdir newmanisha" , "mkdir newmanisha"};
        Runtime  r = Runtime.getRuntime();
        try {
            Process p = r.exec(commandList);
            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
            String line=reader.readLine(); 
            while(line!=null) 
            { 
            System.out.println(line); 
            line=reader.readLine(); 
            } 

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Trying to execute this but nothing happens . I am using ubuntu 13.10.how to do cd in ubuntu using java program. I have used "-c" but it is not working.

  • There are some posts about this. Here is one of them: http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec – Seitaridis Oct 18 '14 at 06:16
  • Also, you forgot to close the input stream. Some caching may happen and on the stream until an explicit flush - but the Java process may end before that. – rlegendi Oct 18 '14 at 07:09
  • btw: if you just want to create directories - you should use the java File class. – Simon Meyer Oct 18 '14 at 08:24

1 Answers1

0

Just replace the commandList related line with the following line. That should work ..

String [] commandList = {"/bin/bash", "-c", "cd /home/atul/Desktop/javaprogramm/ && mkdir newmanisha && mkdir newmanisha"};
Nagakishore Sidde
  • 2,617
  • 1
  • 16
  • 9