Invoking python script from Java - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Invoking python script from Java

gamblor01gamblor01 Posts: 2Member
I am working on a Java program for use at work that is coming along nicely. It is used to analyze log files. For one particular type of log files, we already have a nice python script that will generate all sorts of statistical information about run lengths, the top 10 SQL statements used, the number of rows aggregated and deleted from each table, etc. In a sense -- it's really slick!

Rather than reinvent the wheel I just planned on including it with the Java program that I'm working on right now, and invoking it whenever that particular log needs to be analyzed. Is there a simple way to do this? I have been looking into jython but so far all of the examples that I have found and research I have done suggests that jython is used to invoke Java code from within a python script. Is there a way to do precisely the opposite? I found some information about jythonc but I thought I read somewhere that it was deprecated and no longer supported.


Actually, I know that there is a way to invoke python from within Java and I have done it:

[code]
import java.io.*;

public class Foo
{
public static void main(String[] args)
{
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec("python foo.py");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();
String line = "";
while (br.ready())
System.out.println(br.readLine());

}
catch (Exception e)
{
String cause = e.getMessage();
if (cause.equals("python: not found"))
System.out.println("No python interpreter found.");
}
}
}
[/code]


It works perfectly:


[code]
$ cat foo.py
import os

print "SUCCESS"
$ java Foo
SUCCESS
$ echo "print "ASDF"" >> foo.py
$ cat foo.py
import os

print "SUCCESS"
print "ASDF"
$ java Foo
SUCCESS
ASDF
[/code]


The only problem with this method is that it requires the user to have a python interpreter installed on their system (and in the PATH). Is there some way that I can just include a jar file with the final code and then just call a Java class inside of that jar which will interpret and execute the python script for me? I think jython might be able to do this but I'm not sure.

What about jythonc? Should I be using that? I was able to use it to produce a jar file which seems to be what I want...but every class it compiled has a hard-coded path in it the python script. That's not very portable! I also haven't figured out exactly how to invoke my script yet.


Seems like I just want to include an entire jar file containing jython (there is an option in the installation to make it a jar file). That sounds like what I should be doing -- but I don't know how to use it to invoke my python script from inside of my Java code. Can anyone tell me how to do that?

Comments

  • gamblor01gamblor01 Posts: 2Member
    Nevermind...I figured it out. jythonc isn't very useful because it hard-codes the path to your python script into the java code. This isn't portable at all. However, I installed jython as a jar file and now I can just invoke it by including it in my class path. Here is a sample file:

    [code]
    import org.python.util.PythonInterpreter;

    public class Foo
    {
    public static void main (String[] args)
    {
    try
    {
    PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
    PythonInterpreter interp = new PythonInterpreter();
    interp.execfile("foo.py");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    [/code]


    Just be sure to compile and execute with jython.jar in your classpath:

    [code]
    $ cat foo.py
    print "I'm python"
    print "and I'm called from Java"
    $ javac -cp .:jython.jar Foo.java
    $ java -cp .:jython.jar Foo
    I'm python
    and I'm called from Java
    [/code]
Sign In or Register to comment.