cmd - Java: Executing shell command from java code not working -


i trying run command java code , expecting file generated 1 liner code :

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 

this cmd fine when running cmd line wrong java code gatting hard time figure out , not seeing expected file being generated. any clue whay may have happened here?

here code generate file :

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

thanks in advance.

as realskeptic pointed out, pipe characters (|) not command arguments; they're interpreted shell. , calling command (cut) directly rather using shell.

this isn't direct answer question, can accomplish task without shell commands:

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")) {      (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)) {     (byte sumbyte : sum) {         outputformatter.format("%02x", sumbyte);     }     outputformatter.format(" *%s%n", outputfile); } 

Comments