I supply you with the "calling python from java part". (Thats the one you need right?)
Here is the python code (i.e. the one that should be called from the program)
-
import sys
-
-
# take the first input argument
-
-
mystring = sys.argv[1]
-
-
# reverses the string, but you can do anything you like here..
-
#
-
-
print mystring[::-1]
-
It simply reverses a string and output it to stdout. Now we would like to have a java program that get that info.
-
import java.io.*;
-
-
class SomeJavaProgram{
-
-
public static void main(String a[]) throws IOException{
-
-
Process process = Runtime.getRuntime().exec("python SomePythonProgram.py hello");
-
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-
System.out.println(reader.readLine());
-
reader.close();
-
-
}
-
-
}
-
Save the python program as "SomePythonProgram.py" and the java program as "SomeJavaProgram.java". Compile the java program, and run it.
Its not really a "pretty way" to do it, but it works. I recall vaugly that there was something called Jython, that (I think) was a python interpreter written in java. Maybe you could use this to embedd python in your java programs? (I have never used Jython)
-kudos
Hello,
I'm writing an application that takes pen data and tries to recognize the character and therefore predict the word you are writing. A java file will be returning text to stdout that contains the possible characters that the user just wrote.
I would like to be able to use nltk_lite in python to find possible words based on the bigram and return these possibilities to the java GUI to allow the user to choose the correct word.
I plan on the java and python programs to be sending data and possible next words back and forth between each other. Someone told me about pipes(?) and I couldn't find a simple tutorial online.
All i want to do is pass simple strings between the 2 programs. If anyone could recommend a python module or other way to achieve this it would be really helpful,
Thank you for any help :)
ps: I'm using linux if that makes any difference