¿Cómo hacer que las tuberías funcionen con Runtime.exec ()?
Considere el siguiente código:
String commandf = "ls /etc | grep release";
try {
// Execute the command and wait for it to complete
Process child = Runtime.getRuntime().exec(commandf);
child.waitFor();
// Print the first 16 bytes of its output
InputStream i = child.getInputStream();
byte[] b = new byte[16];
i.read(b, 0, b.length);
System.out.println(new String(b));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
La salida del programa es:
/etc:
adduser.co
Cuando corro desde el shell, por supuesto, funciona como se esperaba:
poundifdef@parker:~/rabbit_test$ ls /etc | grep release
lsb-release
Internet me dice que, debido al hecho de que el comportamiento de la tubería no es multiplataforma, las mentes brillantes que trabajan en la fábrica de Java que producen Java no pueden garantizar que las tuberías funcionen.
¿Cómo puedo hacer esto
No voy a hacer todo mi análisis utilizando construcciones Java en lugar degrep
ysed
, porque si quiero cambiar el idioma, me veré obligado a volver a escribir mi código de análisis en ese idioma, lo cual es totalmente prohibido.
¿Cómo puedo hacer que Java realice canalizaciones y redireccionamiento al llamar a comandos de shell?