JavaFX show looping Python print output - java

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.

Related

Want to know the exact comment

how to run a jar file in background in windows cmd
this below jar file cmd i want to run in background
java -Djavax.net.ssl.trustStore=cacerts_appedo_agent-Djavax.net.ssl.trustStorePassword=changeit -jar appedo_tomcat_agent_2.0.063.jar
You can't do this in a platform-independent manner.
In Unix/Linux, you would call the fork() system call, which duplicates your process. With Java, that means duplicating the entire JVM. Then, you'd have to figure out whether it's the parent process or the child process, which you can determine from the process ID that the fork() call gives you. If it's the parent process, you exit. If it's the child process, you have to close standard input, standard output and standard error.
In Windows, there appears to be the FreeConsole function, but I know next to nothing about Windows programming.
So conceivably, you could write a JNA library that figures out on which platform you are, and invokes the appropriate calls to achieve this. But it's probably not the best idea to get rid of the console window when starting a Java application.
Use javaw, although you won't get any console output either, which is inconvenient.

How to check if a key was pressed in do-while Loop [in Java] [duplicate]

I have a console program written in Java that should respond to single key presses, but the user does not press enter.
I'm making pong.. so need the up and down keys to move the bat thing.
Alternative approaches welcome! (apart from making a GUI instead)
-- Edit:
I'm only going to run my program on UNIX systems (OSX and Linux), so I think I can put the terminal into "raw" mode with this: stty raw
When I type that into the console before running the program it works! But I need Java to do it automatically, so I tried this:
Runtime.getRuntime().exec("stty raw");
and it does nothing... probably because the JVM is just running it as a separate process and not as a process within this terminal.
I think you can't without native code and JNI. Take a look at Java Curses library:
http://sourceforge.net/projects/javacurses/

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.

How to move Java runtime console to different terminal window?

I executed a Java program from the command line in terminal app A. I want to move the console to terminal app B without having to exit and re-execute the program.
I can think of a few potential ways to solve this, ranging from:
A) In Java implement a new InputStream and OutputStream that somehow can be wired to a new process started in terminal app B.
...to
B) Find a way to put the main Java process in terminal app A in the "background" so that original process can be reopened in a terminal app B.
Ideally, I want to be able to "log in to" and "log out of" my Java process from any terminal on my computer. Has anything like this already been accomplished, and which approach would be best to make it myself? I am open to solutions that involve Java code, shell scripts, or both.
My specs:
OSX: 10.12.4
Usually running zsh on iTerm
If I was using Linux, the perfect solution would be reptyr, a command line tool that allows you to easily switch terminal windows.
On Mac, the best solution I have found is screen. It can also be used to switch terminal windows but must be invoked before running java in order to work and seems a lot more complex.

How do I run Gnuplot Scripts from a Java environment?

All of the information I'm finding about using JavaPlot is about how to create plots and run them from a Java code-level. This is fine, but I already have some script files used that do what I want them to outside of Jave when a user executes them. Right now, I can open Gnuplot, and type the commands
gnuplot> load "script.plt"
And it will output me a nice .png graphic of the plot I have. In the script, one of the lines called is
plot "graphData.dat" title "Line Title"
The file 'graphData.dat' stores the co-ordinate information needed to plot my graph.
This works fine when the user double-clicks the 'script.plt' file in its folder (I'm running in a Windows environment). This is pretty flawless (minus the fact that the gnuplot process doesn't end properly, which is not what I'm asking about but if it rings any bells, I'd love to hear how to fix that, too).
The problem I'm facing is that I need to be able to run the script through gnuplot through a java environment. My program needs to automatically export a .png graphic using gnuplot. I've done some looking around, and JavaPlot seems to be the library of choice for Java Support in Gnuplot, but I can't get it to do what I want: simply running an instance of gnuplot that calls the aforementioned load command.
Any help or advice I can get would be appreciated, I've tried forsaking the script file, I've tried to dynamically call the load command (but not from the correct place, apparently), and I've tried plotting the .dat file directly, which it really doesn't like. As long as I can get that script running, I'm set.
One thing that I notice right away: simply loading a script from the gnuplot prompt (i.e.)
gnuplot> #We've already entered gnuplot from the commandline
gnuplot> load "script.plt"
is completely equivalent to passing the script name as the first argument to gnuplot on the commandline (i.e.)
gnuplot script.plt
This is also equivalent to gnuplot -e 'load "script.plt"' from the commandline. This simplifies your problem because now you don't need to figure out how to communicate with gnuplot via pipes, you only need to figure out how to issue a system call in Java -- A task which I would assume is reasonably simple for someone who codes in java. In python, I could do import os; os.system("gnuplot script.plt").
I think the second, third and fourth answers in the link posted by #andersoj gives an examples and links.
JavaGnuplotHybrid library is what you need: hybrid programming with Java and Gnuplot.
Hybrid programming with Java and Gnuplot
Very light weight (just three core Classes)
Use tags in Gnuplot code to execute functions or get fields' values
in Java.
Support both synchronized and asynchronized running of Gnuplot in
Java. (synchronized: your java program will wait until you close the
popped Gnuplot window; asynchronized: you java program will not
wait.)
Capture error/normal text output of Gnuplot to the java terminal
Read Gnuplot code from xml files
Support Gnuplot code template.
For more details:
Project page: https://github.com/mleoking/JavaGnuplotHybrid
Examples: https://github.com/mleoking/JavaGnuplotHybrid/blob/master/javagnuplothybrid/doc/examples.md

Categories