Java/python using processBuilder - java

Good evening all,
am running a python script inside java using processBuilder.
the python script returns a list and i dont know how to get it java and use it since all i can do for the moment with process builder is print errors or outputs.
is it possible to get the list in java as well.
Many thanks

From your scenario, you are looking for inter process communication.
You can achieve this using shared file. Your python script will write the output in text file, and your java program will read the same file.

Related

How can Java Applications communicate with MATLAB

I have some mex files that urgently need to be called via MATLAB, there is currently no way around. However, I really despise MATLAB's GUI (in)possibilities and would like to create some e.g. JavaFX Apps.
My question: how can a Java app's communicate with a running MATLAB instance?
I know that you can include Java objects into MATLAB, however I would prefere to have a standalone Java app.
Java can execute commands via command line for example:
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
So it is possible to execute a MATLAB script via command line in Java.
In MATLAB it is possible to write files with any data needed. I don't remember the exact way you may do this. http://www.mathworks.com/help/matlab/ref/fprintf.html gives an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
It is some kind of a workaround but it should work and it is not really hard to implement.
Update.
If Matlab is already running and you want to communicate with it in another application (Java), it may be done using a network connection through the localhost. Matlab may listen to some predefined port (for code example see http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example ) and do some action when a "start" trigger is sent via Java (or even some data along with the trigger). In Java you may use the Socket class (some code example may be found here http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html ).
Also it may be done writing data into files. For example, Java adds some command to some file with predefined name (command.txt). Matlab scans this file in a loop and when something is found there it starts calculation (and Java application waits for results in some results.txt file).
I would suggest to start a server in Matlab that listens on a specific port to send/receive data to/from a Java client. By using the eval Matlab command you could even invoke scripts/command/etc. remotely controlled by a Java client.
You might want to have a look at this code example.

How do I write to the input stream of an already running java program?

I have a CentOS server which is currently running a java jar application. I need to write a php script to communicate with this running program via its input stream. The java program outputs its output to a log file so I don't need access to the output stream.
I don't want to restart the program, just access the running process and interact with it.
Can someone point me in the right direction?
If portability is not a big matter for you, why not creating your own pipe(s)? I don't know much about the java application but have a look at the "mkfifo" function/command.
First, find the ProcessID of the application. You may do it using:
ps -Af | grep java
Since you are using java, you may feel more convenient with the jps command for finding the PID.
I'll assume PID of the running application is 12345. It suffices to issue the command:
cat >/proc/12345/fd/0
And whatever you type in will be put in the standard input of that application. Note that fd contains the file descriptors used by the application, and I suppose the 0-th file descriptor would always be stdin.
Using PHP for writing into the file (and thus being consumed by the application as input) is possible as well.

How to execute java functions/script in PHP

I have a system which is implemented using PHP(Joomla). Now the client wants to integrate a SMS gateway. Unfortunately SMS gateway does not supoort PHP. It's written in Java and All the examples are written in JAVA.
I'm supposed to include webservices‐rt.jar to my program and run a example script as below.
lk.mobitel.esms.User user = new lk.mobitel.esms.User (); user.setUsername(“TestUser”);
user.setPassword(“Password”);
lk.mobitel.esms.test.ServiceTest st = new lk.mobitel.esms.test.ServiceTest ();
System.out.println(st.testService(u));
According to my knowledge I can run a .jar as below using PHP
<?php exec("java -jar filename.jar arguments",$output); ?>
How can I run a JAVA script like above in PHP? Is it possible? Is there a way to bridge PHP and JAVA? What I want is to run JAVA in PHP else I would say communicate between these two.
I never did this, but PHP Manual gave a link for PHP-Java Bridge for PHP5 as PHP/Java Bridge. If it is what you are looking for ?
It's not possible to combine PHP and Java in the same script and have them executed in one run.
Your exec approach is the way to go, if you are approaching from PHP. But you should rather use passthru as exec does not return anything. With passthru you can get the shell response of your java command and act accordingly.
Is there a way to bridge PHP and JAVA? What I want is to run JAVA in PHP else I would say communicate between these two.
Simple answer:
Bridge using data-exchange layer. :)
Hint (in super simple language): My favorite webservice uses PERL to process their scripts and I fetch them using XML.
Groupon is made on RoR but, I happily get tweets, thanks to their API which outputs a JSON.
Simple? :)

Execute Java program server-side from a Node.js / Heroku app?

Can I call a Java program from a Node.js application on Heroku?
I have a Node.js/Heroku app. But now need to add server-side capability to run an algorithm on an input data file and output data to a JSON format. I already have a Java library that can read the file and run the algorithm, and it would be very difficult (at best) for me to re-write it in pure Node.js.
So, could write a command line program, that takes an input file and pipes the results to stdout, e.g.
java mytask.class -cp ./mylibrary.jar --in /tmp/file.in > output.json
Is it possible to shell out a call to a Java command line program from Node.js? I know one can deploy Java applications to Heroku, but here want to execute a bit of Java from a Node.js app.
Don't you want this and child_process.exec() in particular ?
Node provides a tri-directional popen(3) facility through the
child_process module.
It is possible to stream data through a child's stdin, stdout, and
stderr in a fully non-blocking way.
Note that your example command above isn't right, since you're trying to pipe to a file (output.json). Pipes only work between processes. The child process module would allow you to read the processes' stdout directly and you wouldn't need the file (similarly for the input stream)

Java program for Data Processing and Matlab code for results plotting

I am a beginner in the field of Data Mining. I have a data which i need to process before visualizing any useful results. So I do the following:
Preprocess data using Java Program ( Output : txt files) (Input: FileNames)
Convert .txt files to .xls files ( Excel file)
Import excel file in MATLAB
Write Matlab Script to draw some plots on the imported data
I wish to automate this process by just pressing run button and then it happens automatically.
Please guide me how to integrate my work so instead of doing these 4 steps each time i just provide input file names and all the work is done in one go.
Thanks!
You can use arbitrary Java classes from within MATLAB - it is supported to run user-defined classes and even pass data between them and MATLAB. This would give you the following benefits:
no need to write text files/XLS and then import them into matlab
write everything in one MATLAB script - that is what duffymo had in mind, just that you do not need any shell/batch processing. Everything is done in MATLAB
you could add a MATLAB-implemented GUI to handle the process
Have a look at this SO post for some insights.
You want a scripting language: Windows command if you're on that platform; shell scripts on Linux.
It might not be a button, because that implies a UI. You have to write what amounts to an application for that.
The java matlabcontrol library might come in handy
http://code.google.com/p/matlabcontrol/
I had a similar task some time ago. Matlabcontrol is a library which connects MATLAB to your Java program and is quite easy to use.
All steps can be done in Matlab environment.
Java code can be called in Matlab directly. Just add your java class path to Classpath.txt in Matlab and import in the script code.
Maybe CSV data file is better for your case.

Categories

Resources