0

Este es el ejercicio: Desarrolla un programa JAVA que muestre los recursos del sistema. Nota: usa los comandos: free –mt; df; ps; fdisk –l.

Esto es lo que yo he realizado

Scanner entrada = new Scanner(System.in);
        boolean salir = false;
        int opcion; // Guardaremos la opcion del usuario

        while (!salir) {
            System.out.println("*********************************************");
            System.out.println("1. Comando free-mt");
            System.out.println("2. Comando df");
            System.out.println("3. Comando ps");
            System.out.println("4. Comando fdisk –l");
            System.out.println("0. Salir");
            System.out.println("*********************************************");
            System.out.println("Elija una de las opciones");
            opcion = Integer.parseInt(entrada.nextLine());
            switch (opcion) {
                case 1:
                    Process process = Runtime.getRuntime().exec("ipconfig");
                    InputStream inputstream = process.getInputStream();
                    BufferedInputStream bufferedinputstream = new BufferedInputStream(inputstream);
                    break;
                case 2:
                    Process process2 = Runtime.getRuntime().exec("df");
                    InputStream inputstream2 = process2.getInputStream();
                    BufferedInputStream bufferedinputstream2 = new BufferedInputStream(inputstream2);
                    break;

                case 3:
                    Process process3 = Runtime.getRuntime().exec("ps");
                    InputStream inputstream3 = process3.getInputStream();
                    BufferedInputStream bufferedinputstream3 = new BufferedInputStream(inputstream3);
                    break;
                case 4:
                    Process process4 = Runtime.getRuntime().exec("fdisk –l");
                    InputStream inputstream4 = process4.getInputStream();
                    BufferedInputStream bufferedinputstream4 = new BufferedInputStream(inputstream4);
                    break;
                case 0:
                    salir = true;
                    break;
                default:
                    System.out.println("Solo numeros entre 0 y 4");
            }

        }
    }
}

se que son comandos de linux pero por eso he puesto el ipconfig para probarlo. El problema es que al hecharlo andar no da nada osea el programa finaliza y no sale la informacion del comando por pantalla

2
  • y el método main?
    – Juan Rom
    el 24 nov. 2019 a las 19:31
  • es ese esta todo en el main :')
    – unknow
    el 24 nov. 2019 a las 19:35

1 respuesta 1

2

Necesitas ejecutar los procesos dentro de un bloque try ... catch, y también las lecturas de buffer.

Para no hacer el código tan repetitivo, puedes implementar un método showCommand, al cual le pasas el comando desde el bloque switch directamente.

Por ejemplo:

Scanner entrada = new Scanner(System.in);
boolean salir = false;
int opcion; // Guardaremos la opcion del usuario

    while (!salir) {
        System.out.println("*********************************************");
        System.out.println("1. Comando free-mt");
        System.out.println("2. Comando df");
        System.out.println("3. Comando ps");
        System.out.println("4. Comando fdisk –l");
        System.out.println("0. Salir");
        System.out.println("*********************************************");
        System.out.println("Elija una de las opciones");
        opcion = Integer.parseInt(entrada.nextLine());

        switch (opcion) {
            case 0:
                salir = true;
                break;
            case 1:
                showCommand("ipconfig");
                break;
            case 2:
                showCommand("df");
                break;
             case 3:
                showCommand("ps");
                break;
            case 4:
                showCommand("fdisk –l");
                break;        
            default:
                System.out.println("Solo numeros entre 0 y 4");
        }
    }

Y este sería el método:

private void showCommand (String cmd) {
    Process theProcess = null;
    try
      {
        theProcess = Runtime.getRuntime().exec(cmd);
      }

      catch (IOException e)
      {
         System.err.println("Error en el método exec()");
         e.printStackTrace();  
      }

    try
      {
         BufferedReader inStream = new BufferedReader(new InputStreamReader(theProcess.getInputStream()));  
         System.out.println(inStream.readLine());
      }
      catch(IOException e)
      {
         System.err.println("Error en inStream.readLine()");
         e.printStackTrace();  
      } 
} 

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.