From: Ted B. <te...@et...> - 2001-09-11 00:37:51
|
On Monday 10 September 2001 13:03, Phil Surette wrote: > I'd like to be able to easily bundle jython with other applications so that > I can use the full power of python alongside my java apps. I'd like to be > able to do this without requiring users to perform an installation of > jython - it should be enough just to include the necessary jars. > > Right now, I can include jython.jar with an app and I get lots of > functionality, but I can't access the python libraries. I would _love_ to > be able to include the standard libraries either within jython.jar itself > or in an extra libary (though everything in one jar is definitely the > sweetest solution. > > Has this been done already but I'm just missing it? If no, can you give me > some pointers on where to dive into the code to make this happen? > > - Phil Surette I am shipping the standard modules as part of my application jar. I list jython.jar as a required extension ( placed in lib\ext ) and then append "/fully qualified pathname to my jar!Lib" to the sys.path of the interpreter. Here's the code I use to do so. Just make a call like this: addDirToInterpPath( myInterp, "Lib" ); --- void addDirToInterpPath( org.python.util.PythonInterpreter interp, String subdir ) { interp.exec( "import sys" ); // leading and trailing slashes required to get the URL for a directory. if ( !subdir.startsWith( "/" ) ) subdir = "/"+subdir; if ( !subdir.endsWith( "/" ) ) subdir = subdir + "/"; // get the URL java.net.URL u = MainFrame.class.getResource( subdir ); // make the URL jython friendly StringBuffer buf = new StringBuffer( u.toString() ); // replace the !/ pair with ! so jython can find the directory int index = buf.toString().indexOf( "!/" ); if ( index > -1 ) { buf.replace( index, index + 2, "!" ); } // remove the protocol part of the URL buf.replace( 0, buf.toString().lastIndexOf( ":" ) + 1, "" ); if ( buf.toString().endsWith( "/" ) ) buf.setLength( buf.toString().length() -1 ); // append the path to sys.path interp.exec( "sys.path.append( '" + buf.toString() + "' )" ); } --- ted |