472,779 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,779 developers and data experts.

Three ways to run Python programs from Java

kudos
127 Expert 100+
A week ago, I presented http://bytes.com/topic/python/insigh...hon-together-c. Since then, I realized that perhaps people don't program that much in C. Perhaps people write programs in Java!

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
  1. import java.io.*;
  2.  
  3. class test1{
  4. public static void main(String a[]){
  5. try{
  6.  
  7. String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
  8. BufferedWriter out = new BufferedWriter(new FileWriter("test1.py"));
  9. out.write(prg);
  10. out.close();
  11. int number1 = 10;
  12. int number2 = 32;
  13. Process p = Runtime.getRuntime().exec("python test1.py "+number1+" "+number2);
  14. BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  15. int ret = new Integer(in.readLine()).intValue();
  16. System.out.println("value is : "+ret);
  17. }catch(Exception e){}
  18. }
  19. }
  20.  
We start by making a Java String prg, which contains our Python program, this string is saved on to the file "test1.py". Next, we run the Python interpreter on our system, with the exec method in the Runtime class. We read the output from the output stream returned from the Runtime class, and convert this to an Java int.

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
  1. import java.io.*;
  2.  
  3. class test2{
  4. public static void main(String a[]){
  5. try{
  6.  
  7. String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
  8. BufferedWriter out = new BufferedWriter(new FileWriter("test1.py"));
  9. out.write(prg);
  10. out.close();
  11. int number1 = 10;
  12. int number2 = 32;
  13.  
  14. ProcessBuilder pb = new ProcessBuilder("python","test1.py",""+number1,""+number2);
  15. Process p = pb.start();
  16.  
  17. BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  18. int ret = new Integer(in.readLine()).intValue();
  19. System.out.println("value is : "+ret);
  20. }catch(Exception e){System.out.println(e);}
  21. }
  22. }
  23.  
I saved the above as "test2.java". Then I typed the following;

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
  1. import org.python.util.PythonInterpreter; 
  2. import org.python.core.*; 
  3.  
  4. class test3{
  5. public static void main(String a[]){
  6.  
  7. PythonInterpreter python = new PythonInterpreter();
  8.  
  9. int number1 = 10;
  10. int number2 = 32;
  11.  
  12. python.set("number1", new PyInteger(number1));
  13. python.set("number2", new PyInteger(number2));
  14. python.exec("number3 = number1+number2");
  15. PyObject number3 = python.get("number3");
  16. System.out.println("val : "+number3.toString());
  17. }
  18. }
  19.  
I call this file "test3.java", save it, and do the following to compile it:

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...
Jun 15 '13 #1
7 215711
how do we call python function with one argument by using first or second technique where python code will return string value to java program ?
Sep 23 '16 #2
Oralloy
985 Expert 512MB
I would suggest using the class ProcessBuilder to implement a non-trivial solution that includes communication.

Generating Python code on the fly is a cool thought, but is it really valuable? Most problems that I see involve invoking a sub-process that solves a problem (or class of problems); the code is well developed and debugged, so hoisting to Java (or C) is not considered valuable. Or, hoisting is not reasonable, as the problem easily solved in Python, but not in C/C++/Java.

As for the return value, there are a few options:
  1. Capture the printed output of the Python program.
  2. Have the Python program write the desired output to a well-defined file, and just ignore all other output.
  3. Check sub-process return status and act accordingly.

You might want to use an embedded Python interpreter as part of a web page that executes user entered Python in a restricted environment. For example the page may want to forbid access to all file I/O and system interface commands.

Cheers,
Oralloy!
Sep 25 '16 #3
this worked perfectly for me, but i’m facing another problem, if my python script throws any error like syntax error, the error message is not coming to my java output, i always have to run the script manually and check for any errors. Any idea how to capture that as well
Jun 8 '17 #4
Thank you for classifying different methods of calling Python from Java. I wonder if you have some thoughts about the following:

1) If you use Runtime or ProcessBuilder, is there a way to keep python running between calls and submit new commands to the existing process?

2) If you use Jython, is there a way to load C libraries for python?
Oct 5 '17 #5
canaj7
2
none of these solutions worked.
Jan 24 '18 #6
canaj7
2
I get "Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site"
Jan 24 '18 #7
The solution for first Runtime,Saved my day :)
May 9 '19 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Roberts | last post by:
Hi, I managed to get the Java extension working on php 4.3.10 after following various bits of advice from http://www.thelinuxpimp.com/main/modules.php?op=modload&name=News&file=article&sid=419...
1
by: Harlin | last post by:
Has anyone had any success using py2exe with anything you've written using the Python .NET implementation? (This is the one from the Zope website -- not ActiveState). I seem to be having trouble...
0
by: Christophe | last post by:
hi, I work alone but still use VSS for de version feature.(Version 6.0d build 9848) When I load a project from the start page that is a VSS project, I can't open de option window in de tool menu...
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
28
by: liorm | last post by:
Hi everyone, I need to write a web app, that will support millions of user accounts, template-based user pages and files upload. The client is going to be written in Flash. I wondered if I coudl...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
3
by: Joost | last post by:
Hi guys, I have couple of simple python based active server pages that make use of httplib2 which uses gzip.py. IIS, however, also has a gzip.dll located at the iis/inetsrv path. When using ASP...
34
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas...
1
by: TP | last post by:
Hi everybody, All my problem is in the title. If I try: $ python -c 'print "foo",' It does not change anything, surely because the line return is added by "python -c".
0
by: Venky K Shankar | last post by:
On Sunday 20 July 2008 12:08:49 am Lamonte Harris wrote: you can execute OS system call. here i execute ps -ef and grep the required process name (or you can grep by pid on that particular...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.