Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to call Jython from a Java 6 application using javax.script:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class jythonEx
{
    public static void main (String args[]) throws ScriptException
    {
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine pyEngine = mgr.getEngineByName("python");
        try {
            pyEngine.eval("print \"Python - Hello, world!\"");
        } catch (Exception ex) {
            ex.printStackTrace();
        }       
    }
}

This is causing a NullPointerException:

java.lang.NullPointerException
        at jythonEx.main(jythonEx.java:12)

Does anyone have any idea what I'm doing wrong here?

Edit:

Thanks for the responses! I added jython.jar to the classpath and it runs properly:

java -cp "./;jython.jar" jythonEx
share|improve this question
    
Which one is line 12? pyEngine.eval("");? –  ilikeorangutans Apr 20 '10 at 0:25
    
@ilikeorangutans: Yes, that's the line 12. –  OscarRyz Apr 20 '10 at 0:28
    
That's right. I'm presuming that the ScriptEngineManager can't find the python engine, but I don't see what I'm doing wrong that would cause that... –  griffin Apr 20 '10 at 0:29

2 Answers 2

up vote 13 down vote accepted

You have to register your engine first.

From: ScriptEngineManager.getEngineByName:

[...] first searches for a ScriptEngineFactory that has been registered as a handle [...] Returns null if no such factory was found

The user guide says to use it with JSR-223 you have to:

As of Jython 2.5.1 an implementation of JSR 223 is bundled in jython.jar. Simply add jython to your CLASSPATH and ask for the python script engine.

Did you do that already?

EDIT About your comment: I think you should open a new question, you'll get better answers.

share|improve this answer
    
No, I never did that, thanks! I've used JavaScript through javax.script before without doing anything, so I clearly missed that step. Let me try it now... –  griffin Apr 20 '10 at 0:46
    
@griffin Great!, probably that will do. Let us know if it work would you? –  OscarRyz Apr 20 '10 at 0:48
    
@Oscar: Thanks; that did the trick! –  griffin Apr 20 '10 at 1:21
    
@griffin I'm glad!! good luck with that python + java integration ;) –  OscarRyz Apr 20 '10 at 1:24

You'd probably have to register a ScriptEngineFactory for'python'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.