Keeping input/output separated in Java console application - java

I am writing a console application in Java. It is similar to a chat client: Input and output are asynchronously made. The problem is that if some output is made while the user is in the middle of typing, the lines will get mixed up on the screen.
I am looking for a solution which allows me to have a input area separate from the output area. At the moment I am using an extra thread which polls a BufferedReader on System.in.
The program needs to run on a Linux server and be accessed via an ssh session. So any hints that only work in this environment are fine.
Are there any high level libraries which can do this? Or is there a smart trick using terminal / ANSI codes? The ANSI codes s (save cursor) and r (restore cursor) might be helpful but how do i know where to jump to do the output and how do i handle scrolling?

I recall a long time ago working with similar things but in C++. I was using the ncurses library then. Check out javacurses which seems to be a Java implementation of something like ncurses.

Sounds like you need to use Curses. JCurses is a Java implementation of the Curses library and will give you control of the terminal to allow scrolling, positioning etc.

Related

JavaFX show looping Python print output

I built a GUI in JavaFX with FXML for running a bunch of different Python scripts. The Python scripts continuously collect data from a device and print it to the console as it's collected in a loop at anywhere from around 10 to 70 Hz depending on which script was being run, and they don't stop on their own.
I want the end-user to be able to click a button on my GUI which launches the scripts and lets them see the output. Currently, the best I have done was using Runtime.exec() with the command "cmd /c start cmd /k python some_script.py" which opens the windows command prompt, runs python some_script.py in it, and keeps the command prompt open so that you can see the output. The problem with this is that it only works on Windows (my OS) but I need to have universal OS support and that it relies on Java starting an external program which I hear is not very elegant.
I then tried to remedy this by executing the python some_script.py command in Java, capturing the process output with BufferedReader, creating a new JavaFX scene with just a TextArea in an AnchorPane to be a psuedo-Java-console and then calling .setText() on that TextArea to put the script output in it.
This kinda worked, but I ran into many problems in that the writing to the JavaFX console would jump in big chunks of several dozens of lines instead of writing to it line by line as the Python code was making Print() calls. Also, I got a bunch of NullPointerException and ArrayIndexOutOfBoundsException somewhat randomly in that Java would write a couple of hundred lines correctly but then throw those errors and freeze the program. I'm pretty sure both of these issues were due to having so much data at such high data rates which overflowed the BufferedReader buffer and/or the TextArea.setText() cache or something similar.
What I want to know is what approach I should take at this. I cannot migrate the Python code to Java since it relies on someone else's Python library to collect its data. Should I try to keep with the pseudo-Java-console idea and see if I can make that work? Should I go back to opening a command prompt window from Java and running the Python scripts and then add support for doing the same with Terminal in Mac and Linux? Is there a better approach I haven't thought of? Is the idea of having Java code call Python code and handle its output just disgusting and a horrible idea?
Please let me know if you would like to see any code (there is quite a lot) or if I can clarify anything, and I will try my best to respond quickly. Thank you!
My solution was to still call the Python code from the Java Processbuilder, but use the -u option like python -u scriptname.py to specify unbuffered Python output.

Java: Screen like Implementation for commandline

I have a java application that runs multiple threads where most of them produce unique output that can't and shouldn't be mixed with the output of other's.
In a nutshell I'm wondering if there is anything available that allows me to "switch" inbetween the output of the different threads. For example "Press x to navigate to the output of the next thread"
The linux screen command is basically what comes close to this.
Needs to work in commandline so no swing available.
Any input is highly appreciated.
If not it would end up in a custom build....
If it's only about viewing logs then Andreas's answer would be your best option.
If the actual need is more involved than that, and you wand to build a text-based GUI, check out Lanterna or other suggestions discussed here.
Write the output to separate log files. Now you can tail -f whichever log file you want to view. You can even open multiple command windows and tail multiple logs in parallel.

Write Arduino Serial output to file or program on PC

So I've been looking around for a way to write from an Arduino directly onto a file on the PC, and basically I've found out there's no native way to do so. I wanted to do this in order to then read the file from a C++/Java program, and use the information in it. I also wanted to do this in real-time at some point, so it would be kind of like sending information from the Arduino over to the Java/C++ program for processing.
However, I've seen multiple people state on other forums that you can link the Serial output to some program running on the PC, and then use that program to write the output to a file. However, each time, they neglect to write out how exactly to do this.
The main purpose I wanted to write from the Arduino directly to a file was to read this file from another (Java/C++) program, so the above would be great for me. So how can I get the Serial output into a Java (much more preferably, as I might want to use Swing later) or C++ program, to then use this information in the program itself, or write it to a file? Real-time sending would be a great help.
If the above isn't possible, MATLAB might do, but to be clear, I would much rather be able to interface with Java/C++. Or both Java and MATLAB.
EDIT: To be more specific about what exactly I'd like to do, it is to sort of 'trigger' the Java program to read from the Serial output when a new line has been written (so it reads each line separately) and store it in a string in the Java program, then process it, all at once, and then sleep until another new line is written to the Serial port.
The following Links show you, how you can implement the serial communication in Java and C++.
Java :
http://playground.arduino.cc/interfacing/java
C++ :
http://playground.arduino.cc/Interfacing/CPPWindows
If you want to write the data stream to a local file, you can do that, for instance, with ofstream (C++) or PrintWriter (Java).
Furthermore, there are a several additional libraries for other programming languages such as c# (cmdMessenger).
Just in case anyone was looking for the easiest way to do this, it is hands down to ignore C++ and Java and use MATLAB.
There's a great short tutorial at AllAboutEE that I used and it solved most of my problems. Instead of plotting the data at the end, just use fprintf in MATLAB to output the data to a file.

Java - programming a two-way console window

I am interested in creating a text-based game in Java and am wondering how I would replicate the common server console (ie: nogui server mode in the minecraft-server for example) - as in there is always a line at the bottom of the console for input, and everything else above it is output which can be written to asynchronously.
How would I get started on this? I am unfamiliar with the terminology regarding this sort of stuff and also what libraries to use to begin with.
You should take a look to Java curses library. I haven't use it but, it looks like what you want.
This library may well be helpful: I created it in order to implement a small Rogelike game in Java:
https://github.com/mikera/swing-console
It uses Swing to create a simulated console-style window.

Input output communication between two programs

I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:
=====================
You just need to make sure that stdin for kgsGtp it connected to
the engine's output and stdout for kgsGtp is connected to the engine's
input. Usually, the easiest way to do this is by forking and execing
kgsGtp from within your engine.
=====================
Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.
I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?
That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.
Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.
Edit: more complete example is here: http://support.microsoft.com/kb/190351
What you need is equivalent of POSIX dup() on windows may be this

Categories