martes, 23 de febrero de 2010

Llamar comandos Shell o del sistema desde Java

Alguna vez han pensado hacer llamados del sistemas(códigos shell) desde Java.
Aquí les dejo un curioso código.


public static void main(String args[]) {

String s = null;

try {

// Determinar en qué SO estamos
String so = System.getProperty("os.name");
String comando;
// Comando para Linux
if (so.equals("Linux"))
comando = "ifconfig";
// Comando para Windows
else
comando = "ipconfig";

// Ejcutamos el comando
Process p = Runtime.getRuntime().exec(comando);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));

// Leemos la salida del comando
System.out.println("Ésta es la salida standard del comando:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// Leemos los errores si los hubiera
System.out.println("Ésta es la salida standard de error del comando (si la hay):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

System.exit(0);
} catch (IOException e) {
System.out.println("Excepción: ");
e.printStackTrace();
System.exit(-1);
}
}


Pd:El link donde lo obtuve no recuerdo bien, pero si alguien lo encuentra bienvenido sea :D

2 comentarios:

  1. import java.io.*;
    public class llamadaJava{


    public static void main(String args[]) {

    String s = null;

    try {

    // Determinar en qué SO estamos
    String so = System.getProperty("os.name");
    String comando;
    // Comando para Linux
    if (so.equals("Linux"))
    comando = "ifconfig";
    // Comando para Windows
    else
    comando = "ipconfig";

    // Ejcutamos el comando
    Process p = Runtime.getRuntime().exec(comando);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(
    p.getInputStream()));

    BufferedReader stdError = new BufferedReader(new InputStreamReader(
    p.getErrorStream()));

    // Leemos la salida del comando
    System.out.println("Ésta es la salida standard del comando:\n");
    while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    }

    // Leemos los errores si los hubiera
    System.out.println("Ésta es la salida standard de error del comando (si la hay):\n");
    while ((s = stdError.readLine()) != null) {
    System.out.println(s);
    }

    System.exit(0);
    } catch (IOException e) {
    System.out.println("Excepción: ");
    e.printStackTrace();
    System.exit(-1);
    }
    }
    }

    ResponderEliminar