embedding jython in CPython...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Hargrave

    #1

    embedding jython in CPython...

    I've read that it is possible to compile jython to native code using
    GCJ. PyLucene uses this approach, they then use SWIG to create a Python
    wrapper around the natively compiled (java) Lucene. Has this been done
    before for with jython?

    Another approach would be to use JPype to call the jython jar directly.

    My goal is to be able to script Java code using Jython - but with the
    twist of using Cpython as a glue layer. This would allow mixing of Java
    and non-Java resources - but stil do it all in Python (Jython and Cpython).

    I'd appreciate any pointers to this topic and pros/cons of the various
    methods.


  • Steve Menard

    #2
    Re: embedding jython in CPython...

    Jim Hargrave wrote:[color=blue]
    > I've read that it is possible to compile jython to native code using
    > GCJ. PyLucene uses this approach, they then use SWIG to create a Python
    > wrapper around the natively compiled (java) Lucene. Has this been done
    > before for with jython?
    >
    > Another approach would be to use JPype to call the jython jar directly.
    >
    > My goal is to be able to script Java code using Jython - but with the
    > twist of using Cpython as a glue layer. This would allow mixing of Java
    > and non-Java resources - but stil do it all in Python (Jython and Cpython).
    >
    > I'd appreciate any pointers to this topic and pros/cons of the various
    > methods.
    >
    >[/color]

    Well now that IS getting kinda complicated ...

    AS far a natively compiling Jython scripts ... well, if you natively
    compile them, it'll hard to "script" you java code afterward (I assume
    by scripting you mean loading scripts at runtime that were not know at
    compile time).

    As for using JPype ... well it depends on what you want to script. if
    you Java code is the main app, I'd eschew CPython completely and use
    Jython to script. If you main app is in Python, and the Java code is
    "simply" libraries you wish to use, then I'f go with CPython + Jpype. It
    is very easy to manipulate Java objects that way, even to receive callbacks.

    I guess it all comes down to what you mean by scripting, and exaclt what
    the structure of your application (as far as what is java and non-java).
    If you care to explain your situation a bit more, we'll be better able
    to help you.

    Steve Menard
    Maintainer of http://jpype.sourceforge.net

    Comment

    • johng2001@rediffmail.com

      #3
      Re: JPype and classpath (Was Re: embedding jython in CPython... )

      Thanks for the response. However, I continue to have problems. Allow me
      to give some more detail.

      For simplicity of testing, I hard coded the classpath and JVM path
      (BTW getDefaultJVMPa th() returns None on my system)

      import os, os.path
      from jpype import *

      startJVM("C:/jdk1.5.0/jre/bin/client/jvm.dll",
      "-Djava.class.pat h=D:/Temp/classes")
      ....
      shutdownJVM()


      I have setup a classes folder in the script folder (D:/Temp) and have
      placed test.class in it.
      I run the script from the script folder (working directory is the same
      as script's root path in this case)

      Now how do I load the class test? I am afraid I cannot make that out
      from the docs.

      The simple test class is
      public class test
      {
      public int i = 100;
      }


      What do I have to do before I can write
      test().i
      ?

      Thank you for your time.

      Comment

      • Steve Menard

        #4
        Re: JPype and classpath (Was Re: embedding jython in CPython... )

        johng2001@redif fmail.com wrote:[color=blue]
        > Thanks for the response. However, I continue to have problems. Allow me
        > to give some more detail.
        >
        > For simplicity of testing, I hard coded the classpath and JVM path
        > (BTW getDefaultJVMPa th() returns None on my system)
        >
        > import os, os.path
        > from jpype import *
        >
        > startJVM("C:/jdk1.5.0/jre/bin/client/jvm.dll",
        > "-Djava.class.pat h=D:/Temp/classes")
        > ...
        > shutdownJVM()
        >
        >
        > I have setup a classes folder in the script folder (D:/Temp) and have
        > placed test.class in it.
        > I run the script from the script folder (working directory is the same
        > as script's root path in this case)
        >
        > Now how do I load the class test? I am afraid I cannot make that out
        > from the docs.
        >
        > The simple test class is
        > public class test
        > {
        > public int i = 100;
        > }
        >
        >
        > What do I have to do before I can write
        > test().i
        > ?
        >
        > Thank you for your time.
        >[/color]

        About the getDefaultJVMPa th(), could you send me your system
        information? On windows, JPype uses the contents of the registry to find
        the JVM. Of course, the usefulness of this mechanism is limited byt he
        sample of configurations i can test (I have only one machine). So any
        info you can provide me on yours can only help.

        About the classpath. JPype 0.4 currently cannot import classes that are
        in the "default" package. The fix is easy, simply put your "test" class
        in a package. For exmaple, if you put the class in the package "test",
        the code to load it would be :

        test = jpype.JPackage( "test").tes t


        --
        Steve Menard
        --------------------
        Maintainer of http://jpype.sourceforge.net

        Comment

        Working...