Search code examples
javajythonembedded-script

Why am I failing to resolve public java objects in jython when using "from javax.mail import *"?


I'm not sure if this is a jython bug or a misunderstanding on some nuances of importing java packages/classes with jython 2.5.2.

I have a jython class that executes jython scripts from java. One of these scripts just sends out mail.

Snippet of jython script code:

from javax.mail import *
from javax.mail.internet import *
from java.util import Properties

props = Properties()
props.put("mail.smtp.host", "path.mydomain.com")
session = Session.getInstance(props,None) # Session can't be resolved

So I end up with an exception when trying to resolve Session, which is at javax.mail.Session, and should be covered by the "from javax.mail import * statement"

The Java class that executes the jython looks a bit like this:

static {
    PythonInterpreter.initialize(System.getProperties(), new Properties(), new String[0]);
}

void run(String scriptPath){
    String script; 
    // script is read into this string here from path

    PythonInterpreter python = new PythonInterpreter(null, new PySystemState());
    try (
        python.exec(script);
    ) catch (PyException pyEx){
        // catch
    }
}

The weird thing is if I execute another jython script with explicit imports, like

from javax.mail import Session

Then that works fine, but it will now also be able to resolve Session from the previous script.

I'm using jython-standalone from org.python in the maven repository, version 2.5.2. This code seemed to work just fine in 2.1. Maybe it's initially failing to create the wrappers when I use "import *"?

(I know I should have explicit imports for performance reasons, but this software executes other people's scripts, so I'm trying to make it work in the meantime.)


Solution

  • After a lot of searching, I found out this behavior is actually a bug:

    http://bugs.jython.org/issue1383