Hello, im trying to write to an external program and the inputs and outputs works fine for the first time that im printing the stdin. After this time the program look like it stuck.
Help Please.
Hello, im trying to write to an external program and the inputs and outputs works fine for the first time that im printing the stdin. After this time the program look like it stuck.
Help Please.
import java.io.*;
import java.util.*;
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
String s = null;
try
{
String pmrFormulaFile = "/opt/ericsson/amos/moshell/commonjars/pm/FORMULA_RNC_H_1_0.txt";
String pmrConfigurationFileStr = "/opt/ericsson/amos/moshell/commonjars/pm/CONFIG_RNC_H_1_0.txt";
List<String> lineList = new ArrayList<String>();
lineList.add("pmr -l /home/" + "roisht" + "/" + "RNC028" +
" -f " + pmrFormulaFile + " -c " + pmrConfigurationFileStr +
" -m " + "4");
Process p = Runtime.getRuntime().exec("moshell");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
BufferedWriter stdOutput = new BufferedWriter(new
OutputStreamWriter(p.getOutputStream()));
stdOutput.write(lineList.get(0));
stdOutput.newLine();
stdOutput.flush();
lineList.add("17");
stdOutput.write(lineList.get(1));
stdOutput.newLine();
stdOutput.flush();
lineList.add("18");
stdOutput.write(lineList.get(2));
stdOutput.newLine();
stdOutput.flush();
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
// In this part the program stucked
lineList.add("18");
stdOutput.write(lineList.get(2));
stdOutput.newLine();
stdOutput.flush();
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
}
catch (IOException e)
{
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
post the error that was thrown when compile/running...
Try debugging your program by adding println() statements to show where it is executing and how the variables are changing value.
post the error that was thrown when compile/running...
there is no error, it just get stucked immidiate after the first while ((s = stdInput.readLine()) != null) at the moment it should be null.
Try debugging your program by adding println() statements to show where it is executing and how the variables are changing value.
i tried that and i saw that stdin is never reached to null, and the program stucked at the time that the buffer supose to be empty.
Is it working ok now?
Is it working ok now?
it does not work and i think i ound out the reason.
the readline is waiting to a new line and its not getting the null cause i didnt close the stdout. i cant close the stdout cause i want to use it after so ill have to think about somthing different.
thanks
Can you use the available() method to check before reading to keep from blocking?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.