从java程序执行Ubuntu命令

时间:2014-10-18 06:09:59

标签: java ubuntu

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();
        }
    }
}

尝试执行此操作但没有任何反应。我正在使用ubuntu 13.10.how使用java程序在ubuntu中执行cd。我用过" -c"但它没有用。

1 个答案:

答案 0 :(得分:0)

只需将commandList相关行替换为以下行。这应该工作..

String [] commandList = {"/bin/bash", "-c", "cd /home/atul/Desktop/javaprogramm/ && mkdir newmanisha && mkdir newmanisha"};
相关问题