2015-09-25 35 views
0

我想從運行Java代碼此命令並期待一個文件與1個襯墊代碼生成的Java代碼執行shell命令:的Java:從不工作

cut -d , -f 2 /online/data/test/output/2Zip/brita_ids-*.csv | sort -u | tr -d '\n' | sha512sum > /online/data/test/output/file_name.txt 

這cmd爲正常,當我運行從cmd行,但我的Java代碼錯了,我很難找出困難,我沒有看到預期的文件正在生成。 任何線索可能發生在這裏?

這裏是我的代碼生成文件:

public String executeCommand(String command) { 

    StringBuffer output = new StringBuffer(); 

    Process p; 
    try { 
     LOG.info("Executing cmd : " + command ); 
     p = Runtime.getRuntime().exec(command); 
     p.waitFor(); 
     BufferedReader reader = 
         new BufferedReader(new InputStreamReader(p.getInputStream())); 

        String line = "";   
     while ((line = reader.readLine())!= null) 
     { 
      output.append(line + "\n"); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
     LOG.error("Error in executing cmd : " + command + " \nError : " + e.getMessage()); 
    } 

    return output.toString(); 

} 

在此先感謝。

+0

什麼是錯誤您收到? – Shriram

+0

這可能會更好1)使用ProcessBuilder和2)爲您的命令使用一個字符串集合,而不是一個字符串。請注意,您不顯示如何調用此方法。 –

+0

@patty使用'getErrorStream()'讀取錯誤' –

回答

0

由於RealSkeptic指出,管字(|)是不是命令參數;它們被shell解釋。你直接調用一個命令(cut),而不是使用shell。

這不是直接回答你的問題,但你可以完成你的任務,沒有任何shell命令:

Charset charset = Charset.defaultCharset(); 
MessageDigest digest = MessageDigest.getInstance("SHA-512"); 

try (DirectoryStream<Path> dir = Files.newDirectoryStream(
     Paths.get("/online/data/test/output/2Zip"), "brita_ids-*.csv")) { 

    for (Path file : dir) { 
     Files.lines(file, charset) 
      .map(line -> line.split(",")[1]) 
      .sorted(Collator.getInstance()).distinct() 
      .forEach(value -> digest.update(value.getBytes(charset))); 
    } 
} 

byte[] sum = digest.digest(); 

String outputFile = "/online/data/test/output/file_name.txt"; 
try (Formatter outputFormatter = new Formatter(outputFile)) { 
    for (byte sumByte : sum) { 
     outputFormatter.format("%02x", sumByte); 
    } 
    outputFormatter.format(" *%s%n", outputFile); 
} 
0

謝謝大家,特別是@RealSkeptic和@ qingl97。隨着你的建議,我做了一個小的改變,並且工作。

Process p = Runtime.getRuntime().exec(
      new String[]{"sh","-c",command}); 

p.waitFor() 
0

如果你想獲得輸出,試試這個。的ProcessBuilder將是多個參數更好commans

try { 
     Process process = Runtime 
       .getRuntime() 
       .exec("cut -d , -f 2 /online/data/test/output/2Zip/brita_ids-*.csv | sort -u | tr -d '\n' | sha512sum > /online/data/test/output/file_name.txt"); 
     process.waitFor(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       process.getInputStream())); 
     String line = reader.readLine(); 
     while (line != null) { 
      // print the output to Console 
      System.out.println(line); 
      line = reader.readLine(); 
     } 

    } catch (Exception e1) { 
     e1.printStackTrace(); 
    } 
    System.out.println("Finished"); 

事情是這樣的,如果你想一系列命令

ProcessBuilder builder = new ProcessBuilder(
     "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && 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); 
    } 

的從SOQuestion

+0

空的catch塊是非常糟糕的做法。 – VGR

+0

你真的只需要從SO複製代碼並使用而無需編輯? – Olu

+0

不幸的是,很多人都這麼做。 – VGR