On Tue, Nov 11, 2008 at 5:49 PM, Sharon Lucas <[EMAIL PROTECTED]> wrote:

>
> The Java jar file must be in the CLASSPATH (as I described in my previous
> posting) in order for Jython 2.1 (which is what STAX uses) to be able to
> load classes from it through the underlying JVM.  Using the
> sys.path.append() is for adding Jython/Python modules that you are going to
> import (it doesn't update the CLASSPATH so it cannot be used to add Java jar
> file to the CLASSPATH in Jython 2.1).  Note that in Jython 2.2.1 and later
> there are some improvements in how Java classes can be loaded.   We plan to
> update STAX sometime next year to use a later version of Jython.
>
> Here's another way you can load a Java jar file that works with STAX and
> Jython 2.1 and doesn't require the classpath to be updated when registering
> the STAX service. I put the code to load a jar file in a separate STAX
> function in this sample job.  Then you can include this 'loadJar' STAX
> function in your STAX job and then update your STAX job to call the
> 'loadJar' function specifying the location of your jar file before importing
> classes from the jar file.
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <!DOCTYPE stax SYSTEM "stax.dtd">
>
> <stax>
>
>   <defaultcall function="main"/>
>
>   <function name="main">
>     <sequence>
>
>       <!-- Must load the jar file before importing classes from it -->
>       <call
> function="'loadJar'">'C:\\testing\\Desktop\\jniwrap-2.9.5.jar'</call>
>
>       <script>
>         # Now import your Java classes that are in this jar file
>       </script>
>
>     </sequence>
>   </function>
>
>   <function name="loadJar" scope="local">
>     <function-prolog>
>       Dynamically loads a jar file so that claases can then be imported
> from it.
>     </function-prolog>
>
>     <function-single-arg>
>       <function-required-arg name="jarfile">
>         A string with the fully qualified path name of the jar you want to
> import
>       </function-required-arg>
>     </function-single-arg>
>
>     <sequence>
>       <script>
>         #import all the references we'll need
>         from org.python.core import Py,PySystemState
>         from java.net import URLClassLoader,URL
>         from java.io import File
>         import jarray
>
>         # Add jar file to the package manager
>         PySystemState.packageManager.addJar(jarfile, 0);
>
>         # Now we need to add our own class loader to the system
>
>         # Get the old class loader
>         systemState = Py.getSystemState()
>         oldClassLoader = systemState.getClassLoader()
>
>         # Use the one loading the jython classes if the system state
> instance returns None
>         if not oldClassLoader:
>           oldClassLoader = Py.getClassLoader()
>
>         jarurl = File(jarfile).toURI().toURL()
>         urlArray = jarray.array([jarurl],URL)
>         newClassLoader = URLClassLoader(urlArray,oldClassLoader)
>
>         systemState.setClassLoader(newClassLoader)
>       </script>
>     </sequence>
>   </function>
>
> </stax>
>
> Dear Sharon,

Thanks again and again for helping me by stretching yourself to such an
extent.
I'll give this a try and get back to you in case I face any problem.

Thanks so much.

Cheers,
Rajat
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

Reply via email to