1

I used Netbeans platform to build my application uasing java languge.I need to invoke some python functions into java class using jython was the only way. unfortunatly when I tried to run the program an error showed indicate that the application did not find the following modules

Exception in thread "main" Traceback (most recent call last):

File "script.py", line 13, in <module>
    import re
ImportError: No module named re
  File "script.py", line 14, in <module>
    from string import *
ImportError: No module named string
Java Result: 1 

this is the code in script.py that I want to invoke one of it's method into my java class

#!/pkg/ldc/bin/python2.1

import xml.parsers.expat
import re
from string import *
import sys

How to add these python modules into my application ?

Abrial
  • 421
  • 1
  • 5
  • 20
  • What happens when you execute your script stand alone (i.e., just `jython script.py` from the terminal)? – Vicent Dec 24 '12 at 11:42
  • Is your PYTHONPATH correct? It should include location of python libraries. – tcb Dec 24 '12 at 15:24

1 Answers1

2

The following code runs just fine on my Ubuntu box with Jython 2.7 and Java 1.6 (tested with Eclipse and from the terminal):

package myjythonproject;
import org.python.util.PythonInterpreter;

public class MyJythonProject {

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

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

UPDATE

I've just installed NetBeans 7.2.1 (version for Java SE) on my Ubuntu box, created a new Java project, MyJythonProject, and added the code shown above to the MyJythonProject.java file. Then on the Properties dialog of the project I've selected Libraries in the left pane. In the right pane I've selected the Compile tab, clicked the Add JARF/folder button and selected my jython jar (/opt/jython2.7a2/jython.jar). The I've closed the Dialog and in the Run menu of the main window I've selected Clean and Build Project (MyJythonProject). After that I've run the project and it works like a charm. No Python/Jython plugin required, just tell to your project where the jython.jar is installed.

UPDATE 2

Also notice that Python3 is not supported by Jython so you have to use a Python2.x interpreter.

Vicent
  • 5,322
  • 2
  • 28
  • 36
  • still get error :(.I added jython.jar into my Netbeans library any idea? – Abrial Dec 24 '12 at 11:28
  • Is your IDE well configured? I don't use NetBeans but maybe [this link](http://wiki.netbeans.org/DevelopingJythonAppsUsingNetbeans) can be of help. – Vicent Dec 24 '12 at 11:36
  • the thing is I'm using Netbeans 7.2.1 and to add python plugin I must downgrade to 6.5 which cues me a lot of problem inside my code. Also I tried many ways to add python plugin into Netbeans 7.2. but they all end up with failure. I think there is simple way to configure the path to python library. Like now I downloaded python and refer to it by using Properties props = new Properties(); props.setProperty("python.path", "C:\\Python33"); PythonInterpreter.initialize(System.getProperties(), props,new String[] {""}); unfortunately still get the same error is it because my path syntax ?! – Abrial Dec 24 '12 at 12:05
  • Please, see my update. If it doesn't help you then I'll give up :) Just one more think, AFAIK Python3 is not yet supported by Jython so you should use a Python2.x interpreter. – Vicent Dec 24 '12 at 15:41
  • Thank you so much, it's worked so well :). relay appreciate your time and this useful answer. many many thanks – Abrial Dec 28 '12 at 14:52
  • @Vicent I tried to do the same, but after adding the jython jar, I had to add `guave, constantine, jruby, antlr-runtime, asm` for it to get to the error `java.lang.IncompatibleClassChangeError: Implementing class` Any idea on what I might have done wrong? I'm on ubuntu14.04, use java 1.7, I have both python 2.7 and 3.4 and I'm not sure which one he chooses, but default should be 2.7. – Peter Raeves Jul 31 '14 at 16:48