From: David H. <dav...@gm...> - 2007-10-01 15:00:25
|
On 10/1/07, annegrl <an...@ya...> wrote: > I'm running an Eclipse based Java application, and attempting to run the > Python interpreter to run a python script. > > Here's how I'm setting up the environment: > > .... > Properties props = new Properties(); > props.setProperty("python.path", jars); //jars is set to include my > TestAnne jar, jython.jar > System.setProperty("java.class.path", jars); //just in case > PySystemState.initialize(props, System.getProperties(), new String[]()); > python = new PythonInterpreter(); By Eclipse-based do you mean you're writing plugins for Eclipse or an RCP application? Eclipse uses some magic to ensure that code running in Eclipse can only load classes from plugins that are declared as dependencies. Jython doesn't automatically inherit this magic. I handled this by writing a class loader that can load exactly the same classes a plugin does. I use an instance of that class loader to initialize the PySystemState: state.setClassLoader(pluginLoader); Since PySystemState is fairly static, there may be problems with embedding Jython in different plugins while respecting Eclipse's class loading limitations. You may get into problems there. I create Jython objects from a single plugin, so I don't have any problems. Here's my class loader. It seems that I hardcoded the plugin name; you should probably do it right ;-) Feel free to ask more questions; I'm using Jython extensively in an RCP app, so it's definitely doable. /** * Class loader that is able to load all classes available to this plugin. */ class PluginClassLoader extends ClassLoader { /** * Creates an instance of <code>PluginClassLoader</code>. */ public PluginClassLoader(ClassLoader parent) { super(parent); } /** * Attempt to load the class named by <code>className</code>, first * by delegating to the parent class loader and then by trying each * of the OSGi bundles available to this plugin. * * @throws ClassNotFoundException If the class is not successfully loaded. */ protected Class<?> findClass(String className) throws ClassNotFoundException { try { return super.findClass(className); } catch (ClassNotFoundException e) { Bundle[] bundles = MonitoringPlugin.getInstance().getBundles(); // Look for the class from the bundles. for (int i = 0; i < bundles.length; ++i) { try { return bundles[i].loadClass(className); } catch (ClassNotFoundException e2) { // keep looking } } // Didn't find the class anywhere, rethrow e. throw e; } } } -David |