0

I have a program that runs another Java program and sends commands to the program but it doesnt detect the commands. The program I am sending to the commands to uses JLine so I think that is the problem, it works for other programs that dont use it. This is the code I use to send the commands

// On process build
OutputStream stdin = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(stdin);

// On button click from javafx
dataOutputStream.write(command.getBytes());
dataOutputStream.flush();

I tried DataOutputStream and OutputStream.

The command just doesnt do anything, nothing happens. No output like invalid command message or anything. Reading the output does work and so do errors

I am not able to edit the code of the program I am trying to run

My full code if it helps https://github.com/JustDoom/JustDoomLauncher/blob/master/src/main/java/com/imjustdoom/doomlauncher/justdoomlauncher/process/GameProcess.java#L36 - Process is created and reading the process is handled https://github.com/JustDoom/JustDoomLauncher/blob/50b88bb658eb115f03fd3a9bf827c69147345e45/src/main/java/com/imjustdoom/doomlauncher/justdoomlauncher/application/ConsoleApplication.java#L42 - create custom console in javafx and write to the process

JustDoom
  • 99
  • 9
  • You are correct. The JavaDoc for `Process.getOutputStream()` states `If the standard input of the process has been redirected using ProcessBuilder.redirectInput then this method will return a null output stream.` and JLine will be messing around with the standard input behind the scenes. If you want to use this method of communicating between the apps then remove JLine, or swap to using another communication method like a socket. – sorifiend May 16 '22 at 01:36
  • 1
    Does this answer your question? [Communication between two separate Java desktop applications](https://stackoverflow.com/questions/1680898/communication-between-two-separate-java-desktop-applications) – sorifiend May 16 '22 at 01:36
  • Ah, I cannot remove JLine so that leaves sockets then. thanks – JustDoom May 16 '22 at 01:41
  • Actaully wait, that wont work. I cant edit the other program – JustDoom May 16 '22 at 01:45
  • If you can not edit the code of the other process, and you also can't remove JLine from the other process, then there is not a lot you can do unless JLine has some other way to communicate with it, but I don't know enough about JLine to say. The other code needs to be edited. – sorifiend May 16 '22 at 01:49

1 Answers1

1

I fixed it. I ended up needing to add a newline to the writer before flushing. so I have this now.

dataOutputStream.write(command.getBytes());
dataOutputStream.write("\n".getBytes());
dataOutputStream.flush();

EDIT: The current answer does work but only in what seems like later versions and not older versions for jline

JustDoom
  • 99
  • 9