Here I present three different options running Python programs from Java; first using the (old) Runtime class, then the ProcessBuilder class and finally embedding Python code in Java, with Jython (a Python interpreter, written in Java!)
Runtime approach
First, let take something that is the old way to do it, the Runtime class.
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- class test1{
- public static void main(String a[]){
- try{
- String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
- BufferedWriter out = new BufferedWriter(new FileWriter("test1.py"));
- out.write(prg);
- out.close();
- int number1 = 10;
- int number2 = 32;
- Process p = Runtime.getRuntime().exec("python test1.py "+number1+" "+number2);
- BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
- int ret = new Integer(in.readLine()).intValue();
- System.out.println("value is : "+ret);
- }catch(Exception e){}
- }
- }
I saved the above source code as "test2.java". the I typed the following;
javac test2.java
To execute it:
java test2
Process approach
I think that the Runtime class approach is a bit old. In the newest Java version, the one to use is the ProcessBuilder class. This gives more structure to the arguments.
Expand|Select|Wrap|Line Numbers
- import java.io.*;
- class test2{
- public static void main(String a[]){
- try{
- String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
- BufferedWriter out = new BufferedWriter(new FileWriter("test1.py"));
- out.write(prg);
- out.close();
- int number1 = 10;
- int number2 = 32;
- ProcessBuilder pb = new ProcessBuilder("python","test1.py",""+number1,""+number2);
- Process p = pb.start();
- BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
- int ret = new Integer(in.readLine()).intValue();
- System.out.println("value is : "+ret);
- }catch(Exception e){System.out.println(e);}
- }
- }
javac test2.java
To execute it:
java test2
Jython approach
Java is supposed to be platform independent, and to call a native application (like python) isn't very platform independent.
There is a version of Python (Jython) which is written in Java, which allow us to embed Python in our Java programs. As usually, when you are going to use external libraries, one hurdle is to compile and to run it correctly, therefore we go through the process of building and running a simple Java program with Jython.
We start by getting hold of jython jar file:
http://www.jython.org/downloads.html
(When I wrote this, the latest version was : Jython 2.5.3)
I copied jython-2.5.3.jar to the directory where my Java program was going to be. Then I typed in the following program, which do the same as the previous two; take two numbers, sends them to python, which adds them, then python returns it back to our Java program, where the number is outputted to the screen:
Expand|Select|Wrap|Line Numbers
- import org.python.util.PythonInterpreter;
- import org.python.core.*;
- class test3{
- public static void main(String a[]){
- PythonInterpreter python = new PythonInterpreter();
- int number1 = 10;
- int number2 = 32;
- python.set("number1", new PyInteger(number1));
- python.set("number2", new PyInteger(number2));
- python.exec("number3 = number1+number2");
- PyObject number3 = python.get("number3");
- System.out.println("val : "+number3.toString());
- }
- }
javac -classpath jython-2.5.3.jar test3.java
The next step is to try to run it, which I do the following way:
java -classpath jython-2.5.3.jar:. test3
Now, this allows us to use Python from Java, in a platform independent manner. It is kind of slow, in fact it feels like the slowest of the three presented approaches. Still, it's kind of cool, that it is a Python interpreter written in Java...