0

I'm trying to get input from the console of a .exe process started by a Java script. Nothing appears in the console window, and nothing is read by the program until the process is terminated.

blServ = new ProcessBuilder(blPath + "Blockland.exe", "ptlaaxobimwroe", "-dedicated", "-port " + port, "-profilepath " + blPath.substring(0, blPath.length() - 1)).start();
System.out.println("Attempting to start server...\n" + blPath);
consoleIn = new BufferedReader(new InputStreamReader(blServ.getInputStream()));

'blServ' is a Process object. And yes, the program is starting successfully.

public void blStreamConsole() //called once every 500 milliseconds
{
    String lineStr = "";
    String line = "";
    int lines = 0;
    try
    {
        if (consoleIn != null)
        {
            while ((line = consoleIn.readLine()) != null)
            {
                //if (!line.equals("%"));
                //{
                    lineStr += line + wordSym;
                    lines++;
                //}
            }
        }
    }
    catch (IOException e)
    {
        netOut.println("notify" + wordSym + "ERROR: An I/O exception occured when trying to get data from the remote console. Some lines may not be displayed.");
    }
    if (!lineStr.equals("") && !(lineStr == null))
        netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr);
}

Basically, this method sees if there is more input waiting in the consoleIn object, and if there is, it appends every line it has to another string, and that other string is sent to a client. Unfortunately, it is all sent in one big chunk right when Blockland.exe is closed. Sorry about the indenting issues. The Stackoverflow editor re-arranged all of the code.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hammereditor
  • 39
  • 1
  • 4

2 Answers2

0

It seems to me that there are two possibilities here:

  • readLine blocks, waiting for input (and doesn't return null as you expect). You may be able to fix it by not using BufferedReader and instead using the InputStream

  • The output stream doesn't flush until all the input has been written. Try putting a flush there:

    Also note that if lineStr is null, you'll get a NullPointerException as your code currently is (you need to swap your conditions), but it can't even be null.

    if (!lineStr.isEmpty())
    {
       netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr);
       netOut.flush();
    }
    
Community
  • 1
  • 1
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0
while ((line = consoleIn.readLine()) != null){
     lineStr += line + wordSym;
     lines++;
}

The problem with this piece of code is that it will keep running until the program exits. It will append every single line to lineStr until the program exits (when console.readLine() is null). The whole lineStr is then printed afterwards, containing the whole console.

If you want to continuously print the output, you will need to print it immediatly:

while ((line = consoleIn.readLine()) != null){
     netOut.println(line);
}

You can run this in one separate thread, and it will keep outputting the console to the output stream until the program exits.

Tom Verelst
  • 15,324
  • 2
  • 30
  • 40