i have a program that creates a process with ProcessBuilder and executes an external program (.jar). The external process should receive a String from stdin, convert their characters to lower or upper case and send the converted String thru stdout. The main process reads the String from keyboard, sends it to the external process using a stream and prints the output of the external process. But when i run the main program it seems that it gets stuck with the external process trying to read data from its stdin. How can i fix this, any suggestions? There's another way to accomplish this (sending the String as an argument of the command that executes the external program) but i need to do it using streams.
Here is the code of main program:
public static void main(String[] args) throws IOException {
String str = JOptionPane.showInputDialog("Insert a String");
String[] cmd = {"java", "-jar",
"ejecutable/Transformador2.jar"};
Process process = new ProcessBuilder(cmd).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = process.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
And here is the code of the external process:
public static void main(String[] args) throws IOException {
String str, strConv="";
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
char c;
str = input.readLine();
for (int i=0; i<str.length(); i++) {
c = str.charAt(i);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
if (c == Character.toUpperCase(c))
strConv += Character.toLowerCase(c);
else if (c == Character.toLowerCase(c))
strConv += Character.toUpperCase(c);
}
}
System.out.print(strConv);
}
Thanks in advance.