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 ?

2
  • What happens when you execute your script stand alone (i.e., just jython script.py from the terminal)? Commented Dec 24, 2012 at 11:42
  • Is your PYTHONPATH correct? It should include location of python libraries. Commented Dec 24, 2012 at 15:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

still get error :(.I added jython.jar into my Netbeans library any idea?
Is your IDE well configured? I don't use NetBeans but maybe this link can be of help.
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 ?!
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.
Thank you so much, it's worked so well :). relay appreciate your time and this useful answer. many many thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.