How to create a 2 way communication between Java and Python - java

I am pretty new to both Java and Python, although I have some experience in programming. For an assignement, I need to create a program which uses Java in some way. My project would use Java as an UX, and Python for signal processing and feature extraction, since it has some good tools for that.
However, my question is how to establish communication between both programse. Maybe this question has been asked before, but since I do not know the best terms, I could not find answers.
In my Java Program, I can get the file path to a .csv file, send it to Python, and Python returns the original signals and processed signals. For that, I wrote:
private static void sendPython(String path, JTextField console)
{
String pathPython = "C:\\Users\\gonca\\Desktop\\untitled0.py";
String [] cmd = new String[3];
cmd[0] = "python";
cmd[1] = pathPython;
cmd[2] = path;
Runtime r = Runtime.getRuntime();
try
{
Process p = r.exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while((s = in.readLine()) != null)
{
console.setText(s);
}
}
catch (IOException e)
{
console.setText("Unable to run python script");
}
}
I was thinking of having the py script output the signals in separated lines, with values separated by "," or ";", and using the BufferedRead to read each line, separate the values and create a new ArrayList from the separated values.
However, before starting working harder to do that, I would like to know if that is the best way to proceed, or is there a more efficient way to do it.

There are more ways to do that:
Solution 1:
Use the python library from java with System.loadLibrary, and call the python method. (here is an example using C/C++: Calling a python method from C/C++, and extracting its return value)
Solution 2:
Launch python as another process, and use D-Bus (or something similar) to communicate with it.

Since you have not mentioned how robust your application is I can think of a solution which can be used if you are planning for a higher level architecture.
Create a python based web application (HTTP server) with all logic to process your files.
Create a java app which can communicate via HTTP python server to get the CSV processed information.
Try to avoid Runtime execution of commands with in your codes that faces user as if it not is properly managed there is always a chance for security breach

Related

Executing an expression in a txt file through Java

Hi was wondering if there was a way to execute a line of code from a text file through Java.
For example, let's say I have a text file, and inside that text file it contains an expression
int a = 1;
int b = 2;
int c;
c = a + b;
So in my main class, I have a file object and it does a try/catch
File file = new File("test.txt");
try {
//code here
}catch (FileNotFoundException e) {
System.err.format("File does not exist \n");
}
My goal is to run the code from the test.txt and output the answer. How should I approach this?
Firstly as from the #D.B. commentary:
You might try using the JavaCompiler tool, however, it requires the JDK so if you're turning it in for extra credit make sure your instructor will run it using the JDK not the JRE
It would be possible to compile the while while you run your program. It would be something like:
Read the file.
Compose a valid Java application.
Compile it.
And start it to run on a new thread.
Now, basically you cannot write Java Standard Edition expressions on a file and load the to execute on run time. It is because all java statements must to be parsed and translated to Java Bytecodes and it can only be done before the program to starts running. Except the case where you construct another Java application, compile it and put it to run as a new application invocation, done by you or some Java System Call.
The only thing the Java Virtual Machine can run are Java Bytecodes. So, your text file is not translated to it, then they cannot run. Although, this is not true to languages as Python and Javascript due they do not need to be translated into Machine Language as Bytecodes for the Java virtual Machine and assembly code for the x86 or x64 processor architectures as languages as C++.
You solution is use a interpreted language as Python and Javascript, instead of a compiled languages as Java or C++. As pointed by #that other guy there is the scripting language BeanShell which is a Java-like scripting language, if you are interested to learn it.
References:
https://en.wikipedia.org/wiki/Interpreted_language
https://en.wikipedia.org/wiki/Compiled_language
https://en.wikipedia.org/wiki/Scripting_language
My goal is to run the code from the test.txt and output the answer. How should I approach this?
Within Java, to achieve this goal you can parse the file using your own syntax rules. And as well stated by #Vince Emigh
You'd need some form of parsing and semantic analysis to do this. You are asking us how to write an interpreter, and seeing how you haven't even posted your attempt or specified a specific problem you are having when implementing this, this is faaar too broad
For your example, it would be like:
text.txt
1
2
+
Your Java Source Code file:
File file = new File("test.txt");
try
{
BufferedReader br = new BufferedReader( new FileReader( file ) )
int numbers[] = new int[10];
String line;
while( ( line = br.readLine() ) != null )
{
if( line.contains( "+" ) )
{
... do stuff
}
}
}
catch( FileNotFoundException e )
{
System.err.format("File does not exist \n");
}
finally
{
if( reader !=null )
{
reader.close();
}
}
References:
How to read a large text file line by line using Java?
https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html
https://docs.oracle.com/javase/7/docs/api/java/io/File.html
Is there an eval() function in Java?

How to retrieve graphic card information on Java?

Is there any possible way to retrieve information of my graphic card adapter using Java API?
I know that DirectX can easily do it, however, I just wonder if Java can do this...?
Like picture below.. DirectX finds out GPU adapter integrated to my hardware, and a list of its supporting resolutions.
My problem is, is there an API that Java would do this kind of thing?
I really wonder if Java is able to get information regarding to Video Card.
Thank you.
As #Sergey K. said in his answer there are several ways to do this. One of these is using dxdiag tool (obviously it will only work on Windows) particularly dxdiag /t variant that will redirect the output to a given file. Then you can process that file to get required info:
public static void main(String[] args) {
try {
String filePath = "./foo.txt";
// Use "dxdiag /t" variant to redirect output to a given file
ProcessBuilder pb = new ProcessBuilder("cmd.exe","/c","dxdiag","/t",filePath);
System.out.println("-- Executing dxdiag command --");
Process p = pb.start();
p.waitFor();
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
System.out.println(String.format("-- Printing %1$1s info --",filePath));
while((line = br.readLine()) != null){
if(line.trim().startsWith("Card name:") || line.trim().startsWith("Current Mode:")){
System.out.println(line.trim());
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
Generated file will look like this:
And output will look like this:
-- Executing dxdiag command --
-- Printing ./foo.txt info --
Card name: Intel(R) HD Graphics Family
Current Mode: 1366 x 768 (32 bit) (60Hz)
There are several ways you can do this in Java. But all of them end up using DirectX/OpenGL/C++/WinAPI/whatever as their back-end.
You will need Java bindings for either of this API. Or you can write your code in C/C++ and use it via JNI.

Imagemagick can't open file when executed from Java Servlet

I'm trying to convert files from png's to pdf using imagemagick and Java. I've got everything working to a place when I'm executing imagemagick command to actually merge multiple png's into one pdf. The command itself looks properly, and it works fine when executed in the terminal but my application gives me error showing that imgck can't open the file (even though it exists and I've set permissions to the folder to 777 :
line: convert: unable to open image `"/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001005.png"': No such file or directory # error/blob.c/OpenBlob/2642.
This is my command :
/opt/local/bin/convert "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001005.png" "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/print-1357784001219.png" "/Users/mk/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sch-java/complete-exportedPanel2013-01-1003:13:17.212.pdf"
And my Java code :
String filesString = "";
for (String s : pdfs){
filesString += "\""+ s + "\" ";
}
Process imgkProcess = null;
BufferedReader br = null;
File f1 = new File(pdfs[0]);
//returns true
System.out.println("OE: "+f1.exists());
String cmd = imgkPath+"convert "+ filesString+ " \""+outputPath+outName+"\"";
try {
imgkProcess = Runtime.getRuntime().exec(cmd);
InputStream stderr = imgkProcess.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
br = new BufferedReader(isr);
} catch (IOException e1) {
msg = e1.getMessage();
}
imgkProcess.waitFor();
while( (line=br.readLine() ) != null){
System.out.println("line: "+line);
}
The whole code is executed from a java servlet controller after getting request from a form. Any ideas what can cause this ? I'm using latest imgck, jdk, and osx 10.7 .
A few things:
When spawning anything but really trivial processes, it's usually better to use ProcessBuilder than Runtime.exec() - it gives you much better control
Even with ProcessBuilder, it often works better to write a shell script that does what you need. Then spawn a process to run the script. You get a lot more control in shell script than you do in ProcessBuilder
Remember that a spawned process is not a shell. It can't, for instance, evaluate expressions, or expand shell variables. If you need that, then you must execute a shell (like sh or bash). Better yet, write a shell script as described above
If all you need to do is to execute some ImageMagick commands, it would probably be easier to jmagick, a Java interface to ImageMagick - see http://www.jmagick.org/
Actually, since the you're assembling images into a PDF, the iText library - http://itextpdf.com is probably the best tool for the job, as it is native Java code, does not require spawning a native process, and will therefore be much more portable.
Solved it by adding all arguments to an arrayList and then casting it to String array.
ArrayList<String> cmd = new ArrayList<String>();
cmd.add(imgkPath+"convert");
for (int i=0, l=pdfs.length; i<l; i++){
cmd.add(pdfs[i]);
}
cmd.add(outputPath+outName);
imgkProcess = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));

Communicating to a C++ program from java

I want to execute a external .exe program from within java. The .exe is a CLI application which takes input in runtime( scanf() ) and outputs depending on the input. I can call the program to execute from java using
Process p = Runtime.getRuntime().exec("cmd /c start a.exe");
instead of
Process p = Runtime.getRuntime().exec("cmd /c start a.exe");
But I think it is also possible to call a program from within java. I have my whole program written in C++ just need a GUI which is written in java. There are a few things to notice:=
1) The communication with the .exe should be runtime (not through main(args) )
2) The java program should take the outputs and store in some variable / panel to use for future
3) Program to be executed can differ ( for example user may select a .exe that doesnt take any input at all)
........So basically the java GUI will act as a RuntimeEnv
public void runEXE()
{
String s = null;
try {
Process p = Runtime.getRuntime().exec("cmd /c a.exe");
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
I know there are a lot of questions about this topic out there. But i cant find any of them much useful.
Rather ugly little function that I use. This takes in the command to be passed to Runtime.getRuntime().exec, then saves the results into a String, and returns the String at the end. You can pick whether you only want the last line (or all output) and whether you want to save the stdout or stderr string from the process.
private static String systemResult(String cmd, boolean append, boolean useErr)
{
String result = "";
try{
// spawn the external process
//printCmd(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
LineNumberReader lnr1 = new LineNumberReader(new InputStreamReader(proc.getErrorStream()));
LineNumberReader lnr2 = new LineNumberReader(new InputStreamReader(proc.getInputStream()));
String line;
int done = 0;
while(lnr1 != null || lnr2 != null){
try{
if(lnr1.ready()){
if((line = lnr1.readLine()) != null){
//System.err.println("A:" +line);
if(useErr){
if(append) result = result + line + "\n";
else result = line;
}
}
}else if(done == 1){
done = 2;
}
}catch(Exception e1){
try{ lnr1.close(); }catch(Exception e2){}
lnr1 = null;
}
try{
if(lnr2.ready()){
if((line = lnr2.readLine()) != null){
//System.err.println("====>Result: " + line);
if(!useErr){
if(append) result = result + line + "\n";
else result = line;
}
}
}else if(done == 2){
break;
}
}catch(Exception e1){
try{ lnr2.close(); }catch(Exception e2){}
lnr2 = null;
}
try{
proc.exitValue();
done = 1;
}catch(IllegalThreadStateException itsa){}
}
if(lnr1 != null) lnr1.close();
if(lnr2 != null) lnr2.close();
try{
proc.waitFor();
}catch(Exception ioe){
}finally{
try{
proc.getErrorStream().close();
proc.getInputStream().close();
proc.getOutputStream().close();
}catch(Exception e){}
proc = null;
}
}catch(Exception ioe){
}
return result;
}
You could use JNI as #linuxuser27 suggests, or you could use SWIG which helps to make the process of communicating from Java --> C++ a little less painful.
Google Protocol Buffers would be a good option for Java/C++ interoperability.
Protocol buffers are Google's
language-neutral, platform-neutral,
extensible mechanism for serializing
structured data – think XML, but
smaller, faster, and simpler. You
define how you want your data to be
structured once, then you can use
special generated source code to
easily write and read your structured
data to and from a variety of data
streams and using a variety of
languages – Java, C++, or Python.
I would look at JNI and use some type of IPC to communicate with the C++ project.
Update:
JNI is a way for Java to interface with the underlying native environment the JRE is running on. This method would require you to create a DLL that is loaded into the JRE when your Java program starts. This JNI DLL would then contain a method which could be called from within your Java program that would pass data into the JNI DLL that could then communicate to the C++ project via a named pipe or shared memory.
The named piped would be create using the CreateNamedPipe Win32 API. Within the JNI DLL you would most likely create a server and in the C++ project you would create the client. Note that the server example is multi-threaded but can easily be converted to a single thread model for simplicity.
Note that this is not a simple task. The other answers offer some approaches that are easier, JNA and passing data to the C++ project via stdin.
A couple of things:
First and foremost, if you haven't done so, read this critically important article: When Runtime.exec won't
Next, there are several ways for Java to communicate to other applications, and probably the easiest is via standard input and output streams. Have you tried using these?
Next there's JNI and the easier JNA, but you stated that your C++ program is running via CLI which suggests to me that you have a .NET dll, not a true Windows dll. Is this so? If so, it would make communication between Java and C++ more difficult.
You can execute .exe programs directly. You just need to supply the full path. Relative path will also be ok.
You can use pipes to interface with an external process: Sending Input to a Command, Reading Output from a Command
The solution can be found here
Problem getting output and passing input to a executing process running under java

Interaction between Java App and Python App

I have a python application which I cant edit its a black box from my point of view. The python application knows how to process text and return processed text.
I have another application written in Java which knows how to collect non processed texts.
Current state, the python app works in batch mode every x minutes.
I want to make the python
processing part of the process: Java app collects text and request the python app to process and return processed text as part of a flow.
What do you think is the simplest solution for this?
Thanks,
Rod
I don't know nothing about Jython and the like. I guess it's the best solution if you can execute two programs without executing a new process each time the Java app needs to transform text. Anyway a simple proof of concept is to execute a separate process from the Java App to make it work. Next you can enhance the execution with all that tools.
Executing a separate process from Java
String[] envprops = new String[] {"PROP1=VAL1", "PROP2=VAL2" };
Process pythonProc = Runtime.getRuntime().exec(
"the command to execute the python app",
envprops,
new File("/workingdirectory"));
// get an outputstream to write into the standard input of python
OutputStream toPython = pythonProc.getOutputStream();
// get an inputstream to read from the standard output of python
InputStream fromPython = pythonProc.getInputStream();
// send something
toPython.write(.....);
// receive something
fromPython.read(....);
Important: chars are NOT bytes
A lot of people understimate this.
Be careful with char to byte conversions (remember Writers/Readers are for chars, Input/OutputStreams are for bytes, encoding is necesary for convertir one to another, you can use OuputStreamWriter to convert string to bytes and send, InputStreamReader to convert bytes to chars and read them).
Look into Jython - you can run Python programs directly from Java code, and interact seamlessly back and forth.
Use ProcessBuilder to execute your Python code as a filter:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PBTest {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("python", "-c", "print 42");
pb.redirectErrorStream(true);
try {
Process p = pb.start();
String s;
BufferedReader stdout = new BufferedReader (
new InputStreamReader(p.getInputStream()));
while ((s = stdout.readLine()) != null) {
System.out.println(s);
}
System.out.println("Exit value: " + p.waitFor());
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Expose one of the two as a service of some kind, web service maybe. Another option is to port the python code to Jython
One possible solution is jpype. This allows you to launch a JVM from Python and pass data back and forth between them.
Another solution may be to write the Python program as a filter (reading data from stdin and writing result to stdout) then run it as a pipe. However I do not know how well Java supports this - according to the Sun docs their concept of pipes only supports communication between threads on the same JVM.
An option is making the python application work as a server, listens for request via sockets (TCP).

Categories

Resources