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>


--------------------------------------------------------------
Sharon Lucas
IBM Austin,   [EMAIL PROTECTED]
(512) 838-8347 or Tieline 678-8347




[EMAIL PROTECTED] 
11/11/2008 11:11 AM

To
Sharon Lucas/Austin/[EMAIL PROTECTED]
cc
staf-users@lists.sourceforge.net
Subject
Re: [staf-users] Unable to call Java and Jython from Stax xml Job








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

Please provide your STAX xml file that contains exactly what you're doing, 
as well as your .py file and Java class files so I can try to recreate and 
resolve the problem.


--------------------------------------------------------------
Sharon Lucas
IBM Austin,   [EMAIL PROTECTED]
(512) 838-8347 or Tieline 678-8347



[EMAIL PROTECTED] 
11/11/2008 04:29 AM 


To
Sharon Lucas/Austin/[EMAIL PROTECTED] 
cc
staf-users@lists.sourceforge.net 
Subject
Re: [staf-users] Unable to call Java and Jython from Stax xml Job






Dear Sharon,

Thanks for the mail.
I just resolved the problem with your help you did to me earlier and a bit 
by adding to the STAF's CLASSPATH environment variable the path to one of 
my .jar file which my Jython and Java classes use:

import sys
sys.path.append("C:\\testing") 


Thanks for your kind help.

I also wonder the why STAF wasn't able to load the classes from the Jar 
file when I actually added the path in my xml file by:

sys.path.append("C:\\testing\\Desktop\\jniwrap-2.9.5.jar") 

Ultimately, I had to add this to the STAF Env variable CLASS PATH in the 
STAF.cfg file.


Regards,
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