Hi Mason,

Eclipse plugin are not just similar to OSGi bundles, they are actual OSGi bundles.

In fact, when you use Jython in a "classic" context, you indeed give Jython the JARs via the classpath. You of course need to point JARs that contain the classes and not the source code for it to work.

In an OSGi context, you could dynamically find the path of your JARs and give them to Jython, but OSGi has a special feature that make only part of your JAR packages visible to the outside world. If you don't want to break that, you have to tell him exactly what packages to load (only the one that are exposed).

The method provided by Jeff solves this issue

Kind regards

Le 21/11/2012 17:41, Mason Wardle a écrit :

I'm not sure of the whole context, but I'm trying to do something within an Eclipse plugin (very similar to OSGi bundle, if not equivalent). I had a similar issue where I was able to import the package (in your case org.toto) but not the class. What I was missing was that although the .java file was in my classpath, the .class file was not. Jython could import the package just fine, but the package was empty because I didn't have any class files in there. Any chance this is what you're facing?


On Wed, Nov 21, 2012 at 5:41 AM, Benoît Thiébault <thiebault@artenum.com> wrote:
Hi everyone,

I have an Java application that I wish to control with Jython, and in particular with JyConsole, an open source Swing-based Jython console (http://dev.artenum.com/projects/jyconsole).

My Java application runs however in an OSGi framework (http://www.osgi.org), which makes things a bit more complicated.

I first created an OSGi bundle (i.e. a module in OSGi terminology) which instantiates the Jython interpreter and displays the Swing console.

Following the indications found in this incomplete thread http://osdir.com/ml/lang.jython.user/2005-02/msg00065.html, I tried the following:

final PySystemState sys = Py.getSystemState();
sys.setClassLoader(new JythonClassLoader(getClass().getClassLoader()));

In OSGi, each bundle has its own classloader. This is why I retrieve the current bundle classloader, which I wrap in a JythonClassLoader.

 * Custom classloader, used to make Jython respect it's parent classloader.
 * This will make Java packages made available through a OSGI classloader become accessable in Jython scripts.
public class JythonClassLoader extends ClassLoader {
    public JythonClassLoader(final ClassLoader parent) {

Then assuming that all bundles are already loaded, I retrieve the packages that these bundles expose (a bundle capability) and add the package name to the PySystemState

for (final Bundle bundle : getBundleModel().getContext().getBundles()) {
    final BundleWiring wiring = bundle.adapt(BundleWiring.class);
    for (final BundleCapability capability : wiring.getCapabilities(null)) {
        final Object packageName = capability.getAttributes().get("osgi.wiring.package");
        if (packageName != null) {

For the dynamic aspect of OSGi, where bundles can appear/disappear at runtime, I will deal with it later. One step at a time.

Once this is done, I create the interpreter and display the console.

My problem is that I can load some classes "import org.test.MyClass" but other fail. For instance "import org.toto.MySecondClass" and "from org.toto import MySecondClass" do not work, while "import org.toto" works. The system.out.println command prompts this package that is apparently loaded...

I don't know much about Jython and I struggle to understand what is wrong...

Can anyone help ?

Kind regards,


Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,
SAP, cloud infrastructure, etc. Download 30-day Free Trial.
Pricing starts from $795 for 25 servers or applications!
Jython-users mailing list