From: Paul G. <pau...@so...> - 2002-04-04 12:00:18
|
Forgive me for not replying to this sooner, but it took while for me to sort through code and actually find the relevant answers. We have dealt with most of common Jython embedding issues, and a few less-common ones. This effort was completed a while ago, so the answers are not fresh in my mind. First, let me say that it is unfortunate that the mailing list archives are not searchable, as this theme keeps popping up from time to time, and most of the answers are already there. But I would have a hard time finding the relevant postings myself :-( You can pack python library modules in a jar file. The most compact deployments that I have seen distribute 2 jar files: jython.jar, plus one containing the libraries. It is conceivable that both of these could be packed into a single jar, but I have not tried it. To access python module in a directory "Lib" contained in file JythonLib.jar used this syntax: "/path/JythonLib.jar!Lib" Here is is code that one of my colleagues used to deduce the location of the libraries in the CLASSPATH. It requires no mods to jython, but may not be 100% reliable: // jar:file:$ABSOLUTE_PATH_TO_JYTHON_LIB!/Lib/string.py final URL url = ClassLoader.getSystemResource("Lib/string.py"); // file:$ABSOLUTE_PATH_TO_JYTHON_LIB!/Lib/string.py final StringBuffer pythonpath = new StringBuffer(url.getFile()); // $ABSOLUTE_PATH_TO_JYTHON_LIB!/Lib/string.py pythonpath.delete(0, 5); // $ABSOLUTE_PATH_TO_JYTHON_LIB!/Lib final int len = pythonpath.length(); pythonpath.delete(len - 10, len); // $ABSOLUTE_PATH_TO_JYTHON_LIB!Lib pythonpath.deleteCharAt(pythonpath.length() - 4); Some other things things that you my find useful is to set the cachedir to some directory that you know you have write access to, and to load a jython registry file and perhaps also include jython properties via system properties. Below, I combined some code snippets that demonstrate these thing. Use it as a guide, I am not sure that I pasted all the pieces together properly: HashMap properties = new HashMap(); properties .put("python.path", "/path/JythonLib.jar!Lib"); properties .put("python.cachedir", "/tmp/my_python_cachedir"); InputStream registryFile = ClassLoader.getSystemResourceAsStream("jython.registry"); // registryPre remains empty Properties registryPre = new Properties(); // values in registryPost override those in registryPre Properties registryPost = new Properties(); // load properties into registryPost from file registryPost.load(registryFile); // System properties override the file values registryPost.putAll(System.getProperties()); // more properties are appended here: registryPost.putAll(properties); PythonInterpreter.initialize(registryPre, registryPost, new String[]{}); Hope this helps, -Paul >I would prefer putting the .py files into a jar file and loading >them from the classpath, but from the discussion so far that may >only be available to me if I modify Jython itself which I am >somewhat reluctant to do due to the maintenance problem associated >with future releases of Jython. Although, it doesn't seem too big >a problem to overcome at present.... > -- Paul Giotta Software Architect Technoparkstrasse 1, CH-8005 Zurich. Email: pau...@So... Home Page WWW: http://www.softwired-inc.com *Next stop UBIQUITY!* Office: +41 1 4452370 | Fax: +41 1 4452372 | Mobile: +41 76 389 1180 *** Go Mobile: Out NOW: iBus//Mobile 2.1 *** ***iBus//MessageServer 4.5 with XA extensions*** |