From: Mark Melvin <Mark.M<elvin@ds...> - 2004-10-28 21:04:33
I figured out my problem. I was creating all of my new consoles with a new
PySystemState() object, thus blowing away my initial classloader. Setting
the classloader of the new PySystemState object to my custom classloader
seems to have resolved the issue.
I should be getting my Jython for Java Programmers book tomorrow, but I
have spent over a day on this and can't figure it out...
I am trying to use Jython from a Java program as an embedded interpreter.
Eclipse, actually. Everything seems to be going fine, but I decided I want
to have access to some of *my* classes in the Jython console. No problem.
First of all, I realized the importing them failed. I tried importng
"org.python" and that failed too. So some research led me to add the
jython.jar file to the Jython runtime with the call
PySystemState.add_extdir("path_to/jython.jar"). No problem. Now I can
import and seem seem to be able to access all the org.python stuff.
So, I tried the same thing on my Java classes. I did the same call to
import the directory containing my jar file. Now Jython can import the
packages, but it will not let me access any of the classes or interfaces
inside of them. I get (with package names replaced):
>>>import my.company.packagename as S
<java package my.company.packagename at 23410971>
['Class1', 'Class2', 'Interface1', 'Interface2', '__name__']
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: java package 'my.company.packagename' has no attribute
This happens for everything. I have tried both adding the directory to the
Jar file with the call to PySystemState.add_extdir(), and I have also
created my own classloader and added the bin folder containing all the
class files using the call to PySystemState.add_classdir(). This has the
same behaviour - it can import packages but there is no access to any of
the classes or interfaces.
What is going on here? I have placed some traces on my classloader, and I
see it is only being called to resolve the following classes when I type
the import statement already presented above:
Initializing Jython sub-system...
Jython class loader: org.python.core.exceptions
Jython class loader: org.python.core.PyFunctionTable
Jython class loader: org.python.core.PyRunnable
Jython class loader: java.lang.Object
Jython class loader: org.python.core.Py
Jython class loader: java.lang.String
It uses my classloader to find the standard stuff, but it doesn't seem to
even consult my classloader for the "Class1" I am after - it just fails
with the AttributeError. Does anyone have any ideas?