Why is neccsary to add \n and \r when sending input to a running process?

167 views Asked by At

I am trying to add a Java from to a Visual Prolog generated executable by running it as a process.

I have used this answer to implement the process interaction. My Visual Prolog expects a number from the command line, it even validates it (whether it is a Prolog term or not), but when running from Java, it does not recognize input as valid. Are those scape chars turning my number into a string?

When I try to remove them, my Java program hungs and the Visual Prolog executable does not respond. I think it is like pressing the enter button.

4

There are 4 answers

0
Saher Ahwal On

\r is carriage return CR \n is linefeed LF

many textual protocols require you to use CR+LF and some require to recognize on LF.

It is like starting a newline in text in Windows

0
user2864740 On

\r\n is an EOL (end-of-line) sequence in Windows.

If the input is read line-oriented then the consuming code won't process the line until EOL (or EOF) is sent - this will make the consumer appear to "hang" while it's really juts patiently waiting for the rest of the line. Consider Scanner.nextLine in Java: nextLine won't return until a full line is ready!

Solution: send the EOL with the appropriate input to Visual Prolog; or close the input stream.


Also see also "line.separator", perhaps. However, the various println (print line) methods should already include the appropriate EOL.

0
user207421 On

It's necessary if and only if the target process requires it.

You state that your process 'requires a number from the command line', but if you're feeding it input through its 'stdin' that can't be the case.

Instead it appears to require a number via a line entered to the console.

In either case the word 'line' indicates the need for a line terminator.

0
Thomas Linder Puls On

The referenced example is using pipes (redirecting stdin and stdout) to communicate between the processes, but you say your program expects a number on the command line.