How to get the output code that throws an exe executed from java with runtime?

2

I run a file .exe (for sending images) from java which can pull different output codes depending on the error, this is my class:

public static int SendImg(String ip, int puerto, String rutaIMG, String tipo) {
        Process p = null;
        try {
            Runtime aplicacion = Runtime.getRuntime();
            p = aplicacion.exec("C:\SendImg.exe " + ip + " " + puerto + " " + '"' + rutaIMG + '"' + " " + tipo);
                        } catch (IOException ex) {
            return p.exitValue();
        }
        return p.exitValue();
    }

And so I call it, the problem is that it always returns 0 regardless of whether the transfer failed or not:

int res = Conector.SendImg(cIP.getSelectedItem().toString(), puerto, tRuta.getText(), cbTipo.getSelectedItem().toString());
        if (res == 0) {
            JOptionPane.showMessageDialog(null, "Imagen enviada correctamente");
        }
        if (res == 1) {
            JOptionPane.showMessageDialog(null, "Error enviando imagen producto  A");
        }

        if (res == 2) {
            JOptionPane.showMessageDialog(null, "Error enviando imagen producto B");
        }

        if (res == 3) {
            JOptionPane.showMessageDialog(null, "Error enviando imagen Splash");
        }
        if (res == 6) {
            JOptionPane.showMessageDialog(null, "Parametro no valido sTypeData");
        }
        if (res == 7) {
            JOptionPane.showMessageDialog(null, "No existe la imagen");
        }

If it is correct, the image is sent, if it is not, the image is not sent but I also get zero, is there any other way to get the output code?

EDIT: ZIP file with the executable; SendImg.exe

    
asked by Angel Montes de Oca 16.08.2017 в 00:19
source

1 answer

3

The java.lang.Process.waitFor() method causes the current thread to wait , if necessary, until the process represented by this process object has ended. This method returns immediately if the thread has already finished, if the thread has not yet finished, the calling thread will be blocked until the thread is closed.

With the aforementioned, the value assigned to your variable res is the result returned from the method p.waitFor(); , example:

 try {
        // crea nuevo proceso
        System.out.println("Creando Proceso...");
        Process p = Runtime.getRuntime().exec("notepad.exe");

        System.out.println("Esperando....");
        // obtiene numero de systemcode
        int exitVal = p.waitFor();
        System.out.println("Process exitValue: " + exitVal);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

Normal execution:

Creando Proceso...
Esperando....
Process exitValue: 0

Execution completed by task manager:

Creando Proceso...
Esperando....
Process exitValue: 1
    
answered by 16.08.2017 / 03:39
source