Quantcast

import failures of core java libraries with Jython 2.2.1

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

import failures of core java libraries with Jython 2.2.1

Chris Clark
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: import failures of core java libraries with Jython 2.2.1

claudef
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:    [hidden email]




From:        Chris Clark <[hidden email]>
To:        [hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users



------------------------------------------------------------------------------
Try before you buy = See our experts in action!
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-dev2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: import failures of core java libraries with Jython 2.2.1

Chris Clark
Thanks for taking a look Claude, were you able to use your class path
hacker to import the core libraries I was unable to load? If so, can you
show a demo so I can see what you did.

I've a similar class path hacker in jyjdbc which works for the 2.2 and
2.5 series (I posted something a while back to the list as the Jython
book version doesn't work on all versions of 2.5.*) so I'll probably end
up using that if this is a classpath issue.

One interesting discovery is that whilst awt fails to import, there
appears to be a way to make it work (I saw some code in
http://code.google.com/p/jythonconsole/ which inspired the following
kludge):

    C:\jythonconsole_svnhg>java -jar c:\jython2.2.1\jython.jar
    Jython 2.2.1 on java1.6.0_21
    Type "copyright", "credits" or "license" for more information.
     >>> import java.awt
    Traceback (innermost last):
       File "<console>", line 1, in ?
    ImportError: no module named awt
     >>> from java.awt import Color, Font, Point
     >>> import java.awt
     >>>


NOTE the second attempt to import awt works fine :-S

This then appears to allow import of pawt too. Similarly something can
be done for sql too:

     >>> import java.sql
    Traceback (innermost last):
       File "<console>", line 1, in ?
    ImportError: no module named sql
     >>> from java.sql import Types
     >>> import java.sql
     >>>

That also works! I'll probably go with redundant imports as a workaround
unless anyone has ideas/comments.

Thanks

Chris


------------------------------------------------------------------------------
Try before you buy = See our experts in action!
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-dev2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: import failures of core java libraries with Jython 2.2.1

claudef
Dear Chris,

The present "classPathHack" has nothing to do with the Jython language, its a tweak for the Java classpath itself, which allows to dynamically append a .jar file after the JVM was already initialized. It fixes a few of the classes in Java that might show problems using reflection. This classPathHacker code is well known under Java, I simply rewrote it in a Jython syntax.

If the hack did not work, the issue relates clearly to class loader of the Jython 2.2.1 version. Hope the work-around with the redundant import you found, gives you a chance to live with the problem.  

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:    [hidden email]




From:        Chris Clark <[hidden email]>
To:        [hidden email]
Cc:        [hidden email]
Date:        03/02/2012 16:10
Subject:        Re: [Jython-users] import failures of core java libraries with Jython 2.2.1




Thanks for taking a look Claude, were you able to use your class path
hacker to import the core libraries I was unable to load? If so, can you
show a demo so I can see what you did.

I've a similar class path hacker in jyjdbc which works for the 2.2 and
2.5 series (I posted something a while back to the list as the Jython
book version doesn't work on all versions of 2.5.*) so I'll probably end
up using that if this is a classpath issue.

One interesting discovery is that whilst awt fails to import, there
appears to be a way to make it work (I saw some code in
http://code.google.com/p/jythonconsole/ which inspired the following
kludge):

   C:\jythonconsole_svnhg>java -jar c:\jython2.2.1\jython.jar
   Jython 2.2.1 on java1.6.0_21
   Type "copyright", "credits" or "license" for more information.
    >>> import java.awt
   Traceback (innermost last):
      File "<console>", line 1, in ?
   ImportError: no module named awt
    >>> from java.awt import Color, Font, Point
    >>> import java.awt
    >>>


NOTE the second attempt to import awt works fine :-S

This then appears to allow import of pawt too. Similarly something can
be done for sql too:

    >>> import java.sql
   Traceback (innermost last):
      File "<console>", line 1, in ?
   ImportError: no module named sql
    >>> from java.sql import Types
    >>> import java.sql
    >>>

That also works! I'll probably go with redundant imports as a workaround
unless anyone has ideas/comments.

Thanks

Chris


------------------------------------------------------------------------------
Try before you buy = See our experts in action!
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-dev2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users



------------------------------------------------------------------------------
Try before you buy = See our experts in action!
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-dev2
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Loading...