From: Humbel O. <Otm...@bi...> - 2001-09-10 12:31:36
|
Hello, when there is an 'invalid' .jar file in the classpath from where java can load classes, but from which the jython package manager cannot determine packages (other people have told us about similar problems with the jar utility here), I would nonetheless like to be able to import single java classes. The problem manifests as follows (package CH.obj.Libraries.Servlet lives inside the offending bisonInfra.jar): [appl|pwe] > jython *sys-package-mgr*: processing modified jar, 'E:\java\lib\bisonInfra.jar' *sys-package-mgr*: skipping bad jar, 'e:\java\lib\bisonInfra.jar' Jython 2.0 on java1.3.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> from CH.obj.Libraries.Servlet import ServletSessionContext Traceback (innermost last): File "<console>", line 1, in ? ImportError: No module named Servlet >>> from java.lang import Class >>> clazz = Class.forName( "CH.obj.Libraries.Servlet.ServletSessionContext" ) >>> clazz <jclass CH.obj.Libraries.Servlet.ServletSessionContext at 7698425> >>> For an 'intuitive' Jython user like me, there is no obvious reason why this import should fail ? My motto is: Jython can import everything java can load ! With the following lines added just at the beginning of method importFromAs in org/python/core/imp.java (from the 2.0 codebase): public static void importFromAs(String mod, String[] names, String[] asnames, PyFrame frame) { if ( names.length==1 && asnames.length==names.length && asnames[0].equals(names[0]) ) { // this is a candidate for simple java class import String packageName = mod; String fullClassName = packageName + "." + names[0]; try { Class.forName( fullClassName ); PySystemState.add_package( packageName ); } catch( Throwable t ) {} } // rest of method left untouched // ... } the problem above would be solved. These lines do not at all claim to be perfect, they are thought just to show that it could be done: [appl|pwe] > jython *sys-package-mgr*: processing modified jar, 'E:\java\lib\bisonInfra.jar' *sys-package-mgr*: skipping bad jar, 'e:\java\lib\bisonInfra.jar' Jython 2.0 on java1.3.0 (JIT: null) Type "copyright", "credits" or "license" for more information. >>> from CH.obj.Libraries.Servlet import ServletSessionContext >>> Please let me know what you think. Do not hesitate to blame me if this is ... Have fun, Oti. |