How to pass inputs to a process with OutputStream?

0

Hello, good night, everyone

After spending some time researching and finding nothing about the topic, or maybe you just do not know how to look I come to ask a question about an exercise I have in class.

The exercise says that I must generate a child process that receives input characters from the parent class, capitalizes them, and the parent class has to output the result on the screen.

Here I post as I have the child process (it is exported as runnable jar).

public class Mayusculas {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub



    InputStreamReader entrada = new InputStreamReader(System.in);
    BufferedReader teclado = new BufferedReader (entrada);
    String cadena = teclado.readLine();

    System.out.println(cadena.toUpperCase());

}}

Well, this class I have as jar, and I pass it as arguments to this:

public class MayusculasPadre {

public static void main(String[] args) throws IOException {

    Process process = new ProcessBuilder(args).start();

    InputStream out = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(out);
    BufferedReader br = new BufferedReader(isr);


    String linea;
    linea=br.readLine();
    System.out.println("Linea:"+linea);
    br.close();

}}

The fault is that the entry to the process is not weighed.

Would someone know how to do it?

Thanks

    
asked by Wolobi 29.10.2018 в 21:23
source

1 answer

0

The problem I had has been solved. Here I leave the solution in case it is worth someone.

public class MayusculasPadre {

public static void main(String[] args) throws IOException {

    System.out.print("Introducir texto: ");
    //Método mejorado para introducción de caracteres por teclaro.
    String texto = utilidades.Entrada.cadena();

    //Ruta y argumentos para lanzar aplicación
    String arg1 = "java";
    String arg2 = "-jar";
    String arg3 = "C:\Users\Alumno\Desktop\mayusculas.jar";

    String[] param = { arg1, arg2, arg3 };
    Process process = new ProcessBuilder(param).start();

    //Escribir en la escucha del proceso
    OutputStream os = process.getOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(os);
    PrintWriter pw = new PrintWriter(osw);
    pw.print(texto);
    pw.close();

    //Caputrar la salida del proceso
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    String linea = br.readLine();
    System.out.println("Linea:" + linea);
    br.close();
}

Thank you all for the time.

    
answered by 15.11.2018 в 00:31