pipe commandes Unix ensemble en Java

J’ai l’obligation d’exécuter la commande ci-dessous à partir de Java

echo  | iconv -f utf8 -t Cp930 

Lorsque j’utilise le code ci-dessous pour exécuter la commande, je constate que seule la partie écho est exécutée, mais que la tuyauterie ne se produit pas

 public static Ssortingng callInconverter2(Ssortingng input,Ssortingng codePage) throws IOException { try{ // Ssortingng command = "echo asdasdasd | iconv -f UTF-8 -t Cp930"; Process p = Runtime.getRuntime().exec("echo "+input+"| iconv -f UTF-8 -t "+codePage); Ssortingng s = null; BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); SsortingngBuilder sb = new SsortingngBuilder(); // read the output from the command System.out.println("Here is the standard output of the command:\n"); while ((s = stdInput.readLine()) != null) { sb.append(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { sb.append(s); } return sb.toSsortingng(); } catch (IOException e) { System.out.println("exception happened - here's what I know: "); e.printStackTrace(); return e.getMessage(); } }} 

Je suis nouveau sur Runtime. Est-ce que quelque chose me manque?

Essayé la méthode suggérée par Thomas

 Ssortingng command = "echo asdasdasd | iconv -f UTF-8 -t Cp930"; Process p = Runtime.getRuntime().exec("bash -c \""+command+"\""); 

obtenait une erreur asdasdasd: -c: ligne 0: EOF inattendu en cherchant la correspondance avec asdasdasd: -c: ligne 1: erreur de syntaxe: fin inattendue du fichier

est-ce que quelque chose me manque

Exécutez un shell avec cette commande – bash, tcsh, quel que soit celui que vous utilisez normalement.

 bash -c "echo | iconv -f utf8 -t Cp930" // or bash -lc "echo | iconv -f utf8 -t Cp930" 

La tuyauterie est une fonctionnalité de shell.

Ainsi:

 Runtime rt = Runtime.getRuntime(); Ssortingng cmd = "echo | iconv -f utf8 -t Cp930"; rt.exec("bash -c \""+cmd+"\""); 

Voir le manuel bash pour les options d’appel. http://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html#Invoking-Bash