【问题标题】:Executing Ubuntu commands from java program从 java 程序执行 Ubuntu 命令
【发布时间】:2014-10-18 06:09:59
【问题描述】:
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 to do cd in ubuntu using java program。我使用了“-c”,但它不起作用。

【问题讨论】:

  • 有一些关于这个的帖子。这是其中之一:stackoverflow.com/questions/5928225/…
  • 另外,您忘记关闭输入流。在显式刷新之前,可能会在流上发生一些缓存 - 但 Java 进程可能会在此之前结束。
  • 顺便说一句:如果您只想创建目录 - 您应该使用 java File 类。

标签: java ubuntu


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 2014-03-20
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多