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
Related
I'm using the 3dmod program of IMOD which is coded in Java (using Qt I think). For my project, I often need to open multiple .mrc files (3D reconstruct) as well as the model I've put on it using a command like the following:
3dmod -E 1U my_3d_object_part1.mrc my_model_part1.mod
After doing all the parts (around 5-10), I need to open all of them to compare.
Are there any ways to create a shell script to do this? Knowing that file names are almost identical, only the number 1 will have to change.
Note that the doc says "3dmod will accept some Qt options (such as -style)". I don't know what those options do or if they can control the window position at launch. I am hoping I could distribute my model windows across my screen to see them all at once.
I am concerned this may not be possible. Are there any ways to control the initial window position at opening?
Qt5 applications can set their initial size/position using the qwindowgeometry flag, like this:
./myApp -qwindowgeometry 640x480+50+50
EDIT:
On Qt4, and I think only if you're using X11, the flag is named -geometry.
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/
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.
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.
I need to be able to change the working directory in MATLAB without interacting with the command window. I'm launching MATLAB from a Java application. Right now the only solution I've come up with is closing MATLAB, changing directory from JAVA and relaunching. Is there some streamlined way to send MATLAB the 'cd' command from JAVA? Doing so from the command-line would also work, since I could use getRuntime().exec(command)
Thanks!
You can do this using JMI if you're using the same JVM as Matlab (if not then I have no idea). There is not much online info about this (it's WAY undocumented/unsupported). Google it or read this: http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. In short, you need to include Matlab's relevant JAR file and then use com.mathworks.jmi.Matlab's functionality. For example:
Matlab.evalConsoleOutput("cd('C:\Program Files\')");
Yair Altman
http://UndocumentedMatlab.com
I am working with Stephen Poletto who posted the original question. There wasn't any existing solution that met our needs so we wrote our own solution based off of Kamin Whitehouse's work mentioned by Yair. It is available for all to use at matlabcontrol.googlecode.com
It allows for controlling MATLAB from a Java program launched outside of MATLAB.