From: <cl...@br...> - 2012-02-03 01:08:32
|
Dear Chris, I often use this is classPathHacker code when some Java classes are not loading correctly through the standard Jython class loader and sys.path.append("...") statement. Try this: import sys from org.python.util import PythonObjectInputStream from org.python.core import * from java.net import * from java.io import * from java.lang import * sysProp = System.getProperties() userdir = sysProp.getProperty("user.dir") userdir = userdir.replace("\\", "/") try: # dynamically adds CLASSPATH for Java Class Loader myclassloader = classPathHacker() url_a = myclassloader.addFile(userdir + "/some_package_path/some_package.jar") except Exception, e: print ("ClassPathHacker Load .jar Error:" + str(e)) import Java stuff now.. The class file: import java.lang.reflect.Method from java.io import File as javaFile from java.net import URL from java.net import URLClassLoader from java.lang import Class as javaClass from java.lang import ClassLoader as javaClassLoader from java.lang import Object as javaObject from java.lang import System import jarray class classPathHacker(): def __init__(self): # init actions self.DEBUG = 0 pass def addURL (self, u): DEBUG = 0 ################################## # Purpose: Call this with u= URL for # the new Class/jar to be loaded ################################# parameters = jarray.array([URL], javaClass) sysloader = javaClassLoader.getSystemClassLoader() sysclass = URLClassLoader method = sysclass.getDeclaredMethod("addURL", parameters) a = method.setAccessible(1) jar_a = jarray.array([u], javaObject) if DEBUG: print ("jar added is : " + str(jar_a)) b = method.invoke(sysloader, jar_a) sysProp = System.getProperties() if DEBUG: print ("------- Resulting CLASSPATH modified by classPathHacker-------" + "\n") if DEBUG: print (sysProp.getProperty("java.class.path")) return u def addFile (self, s): ############################################# # Purpose: If adding a file/jar call this first # with s = path_to_jar ############################################# # make a URL out of 's' f = javaFile(s) u = f.toURL() a = self.addURL(u) return a if __name__ == "__main__": DEBUG = 0 if DEBUG: print "callPathHacker is active" Regards, Claude Claude Falbriard Certified IT Specialist L2 - Middleware AMS Hortolândia / SP - Brazil phone: +55 13 9760 0453 cell: +55 13 8117 3316 e-mail: cl...@br... From: Chris Clark <Chr...@ac...> To: Jyt...@li... Date: 02/02/2012 21:57 Subject: [Jython-users] import failures of core java libraries with Jython 2.2.1 I've discovered an odd Jython 2.2.1 specific problem, importing java.sql or java.awt fails. Jython 2.2rc1 doesn't seem to have this problem (at least for java.sql). Jython 2.5.2 works fine for both too. I breifly tried to use org.python.core.JavaImporter().load_module('java.sql'), whilst this doesn't error, it doesn't seem to offer access to those libs. The awt problem appears to show a bug with the "pawt" package that is shipped with Jython. Any ideas? Any workarounds or tips on how to use JavaImporter? Sometimes it is possible to "import java" and then access "java.sql", etc. but this doesn't work here either. Here is a sample session from a Windows 7 machine: C:\jython2.2.1>java -jar jython.jar Jython 2.2.1 on java1.6.0_21 Type "copyright", "credits" or "license" for more information. >>> import java >>> import java.sql Traceback (innermost last): File "<console>", line 1, in ? ImportError: no module named sql >>> import java.math >>> import java.lang >>> import java.util >>> import java.awt Traceback (innermost last): File "<console>", line 1, in ? ImportError: no module named awt >>> import pawt Traceback (innermost last): File "<console>", line 1, in ? File "C:\jython2.2.1\jython.jar\Lib/pawt/__init__.py", line 2, in ? ImportError: cannot import name awt Chris ------------------------------------------------------------------------------ Keep Your Developer Skills Current with LearnDevNow! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-d2d _______________________________________________ Jython-users mailing list Jyt...@li... https://lists.sourceforge.net/lists/listinfo/jython-users |