Ver Mensaje Individual
  #3 (permalink)  
Antiguo 20/06/2007, 06:41
carota
 
Fecha de Ingreso: abril-2007
Mensajes: 31
Antigüedad: 17 años
Puntos: 0
Re: Ejecutar comando linux desde Java

Lo resolvi de la siguiente forma viendo un programa de otro foro:
Si bien no entiendo todo lo que hace despues, pongo por un lado como anda y por otro todo el resto de los comandos que ejecuta ese programita para ver que hace.

CON ESTO ANDA IMPECABLE !! EN LUGAR DE PONER bash -c HAY QUE PONER SOLO SH !!

String[] command = {"sh","-c","rm *.txt"};
final Process process = Runtime.getRuntime().exec(command);

DE ACA FUE DONDE SAQUE SOLO ESE CODIGO, PERO NO ENTIENDO BIEN QUE HACE.

import java.io.*;

public class carota
{
public static void main(String[] args)
{
try
{
String[] command = {"sh","-c","bzip2 -dc *.tar.bz2 | tar -x"};
final Process process = Runtime.getRuntime().exec(command);
new Thread()
{
public void run()
{
try{
InputStream is = process.getInputStream();
byte[] buffer = new byte[1024];
for(int count = 0; (count = is.read(buffer)) >= 0;)
{
System.out.write(buffer, 0, count);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}.start();
new Thread()
{
public void run()
{
try{
InputStream is = process.getErrorStream();
byte[] buffer = new byte[1024];
for(int count = 0; (count = is.read(buffer)) >= 0;)
{
System.err.write(buffer, 0, count);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}.start();

int returnCode = process.waitFor();
System.out.println("Return code = " + returnCode);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}