From: Anders D. <and...@st...> - 2002-01-02 16:26:15
|
Hello! I keep getting exceptions when I try to load (java) classes dynamically into jython :( My code is as follows: ... // Load jython interpreter with correct classloader try { String className = "org.python.util.PythonInterpreter"; ClassLoader loader = TopManager.getDefault().currentClassLoader(); Class loaded = loader.loadClass(className); pythonInterpreter = (PythonInterpreter) loaded.newInstance(); // Add all packages that is mounted in explorer: if (PySystemState.packageManager instanceof SysPackageManager) { SysPackageManager manager = (SysPackageManager) PySystemState.packageManager; updateJythonClasspath(manager); } ... private void updateJythonClasspath(SysPackageManager manager) { Repository repository = TopManager.getDefault().getRepository(); FileSystem[] fileSystems = repository.toArray(); for (int i = 0; i < fileSystems.length; i++) { FileSystem fileSystem = fileSystems[i]; // FIXME: what about non-local filesystems, cvs? if (JarFileSystem.class.isAssignableFrom(fileSystem.getClass())) { JarFileSystem jarFile = (JarFileSystem) fileSystem; manager.addJar(jarFile.getJarFile().getPath(), true); } else if (LocalFileSystem.class.isAssignableFrom(fileSystem.getClass())) { LocalFileSystem localFile = (LocalFileSystem) fileSystem; manager.addJarDir(localFile.getRootDirectory().getAbsolutePath(), true); } } } What am I doing wrong, not doing? thanks in advance /Anders Example stack trace: "# Jython file - http://www.jython.org/ print "hello world!" from hejsan import * nada = Nada() print nada.hej() " File "<string>", line 5, in ? java.lang.NullPointerException at org.python.core.PyJavaClass.setConstructors(PyJavaClass.java) at org.python.core.PyJavaClass.initConstructors(PyJavaClass.java) at org.python.core.PyJavaClass.__call__(PyJavaClass.java) at org.python.core.PyObject.__call__(PyObject.java) at org.python.pycode._pyx3.f$0(<string>:5) at org.python.pycode._pyx3.call_function(<string>) at org.python.core.PyTableCode.call(PyTableCode.java) at org.python.core.PyCode.call(PyCode.java) at org.python.core.Py.runCode(Py.java) at org.python.core.Py.exec(Py.java) at org.python.util.PythonInterpreter.exec(PythonInterpreter.java) at org.netbeans.modules.scripting.JPythonScriptType.exec(JPythonScriptType.java :154) at org.netbeans.modules.scripting.AbstractScriptType.exec(AbstractScriptType.ja va:140) at org.openide.execution.ScriptType.exec(ScriptType.java:80) at org.netbeans.modules.scripting.ScriptSupport$1.run(ScriptSupport.java:72) at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:118) java.lang.NullPointerException: java.lang.NullPointerException at org.python.core.Py.JavaError(Py.java) at org.python.core.PyTableCode.call(PyTableCode.java) at org.python.core.PyCode.call(PyCode.java) at org.python.core.Py.runCode(Py.java) at org.python.core.Py.exec(Py.java) at org.python.util.PythonInterpreter.exec(PythonInterpreter.java) at org.netbeans.modules.scripting.JPythonScriptType.exec(JPythonScriptType.java :154) at org.netbeans.modules.scripting.AbstractScriptType.exec(AbstractScriptType.ja va:140) at org.openide.execution.ScriptType.exec(ScriptType.java:80) at org.netbeans.modules.scripting.ScriptSupport$1.run(ScriptSupport.java:72) [catch] at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:118) |
From: <bc...@wo...> - 2002-01-03 14:48:37
|
[Anders Dahlberg] >Hello! > >I keep getting exceptions when I try to load (java) classes dynamically into >jython :( > >My code is as follows: >... > // Load jython interpreter with correct classloader > try { > String className = "org.python.util.PythonInterpreter"; > ClassLoader loader = >TopManager.getDefault().currentClassLoader(); > Class loaded = loader.loadClass(className); > pythonInterpreter = (PythonInterpreter) >loaded.newInstance(); > > // Add all packages that is mounted in explorer: > if (PySystemState.packageManager instanceof >SysPackageManager) { > SysPackageManager manager = (SysPackageManager) >PySystemState.packageManager; > updateJythonClasspath(manager); > } >... > > private void updateJythonClasspath(SysPackageManager manager) { > Repository repository = TopManager.getDefault().getRepository(); > FileSystem[] fileSystems = repository.toArray(); > > for (int i = 0; i < fileSystems.length; i++) { > FileSystem fileSystem = fileSystems[i]; > > // FIXME: what about non-local filesystems, cvs? > if (JarFileSystem.class.isAssignableFrom(fileSystem.getClass())) >{ > JarFileSystem jarFile = (JarFileSystem) fileSystem; > manager.addJar(jarFile.getJarFile().getPath(), true); > } else if >(LocalFileSystem.class.isAssignableFrom(fileSystem.getClass())) { > LocalFileSystem localFile = (LocalFileSystem) fileSystem; > >manager.addJarDir(localFile.getRootDirectory().getAbsolutePath(), true); > } > } > } > >What am I doing wrong, not doing? The code above seems to add different paths to the packagemanager. It will probably allow jython to know about the "hejsan" package, but in itself it does not do anything to allow jython to import the java classes in these packages. I'm not sure that the trick you do to load the PythonInterpreter with the right classloader is enough. I suppose it depends on the implementation of the classloader but I suspect that the rest of jython.jar will still be loaded by one of the TopManager classloaders parents. Maybe you can verify or dismiss that theory by loading the class org.python.core.Py using the same trick and checking which classloader it was loaded by. If my guessing is correct, a possible solution might be to remove jython.jar from the parent classloaders reach and make sure that you load jython.jar yourself using an URLClassLoader. Another option is the PySystemState.setClassLoader(..) method, but calling that will create a very closed environment where all java imports must be served by the classloader. OTOH when sys.classLoader is set, it doesn't matter which classloader that loaded the jython.jar code. As you can see, jython's support for foreign classloaders still have a way to go before it reach perfection. regards, finn |