embedding jython in CPython...

J

Jim Hargrave

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.
 
S

Steve Menard

Jim said:
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.

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
 
J

johng2001

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

While we are on topic, I am having some trouble understanding JPype
classpath. How do I init the JVM with the folder in which the Python
program is located included in the classpath?

I tried
t = JPackage('.').test

That did not work.

My environment variable includes current folder in the classpath

I tried passing it as an argument to startJVM. Didn't help.

I think my main use is going to be using CPython with a few Java custom
classes and if anyone has a snippet on this it would really help me.
Thanks
 
J

Jim Hargrave

Sorry - should have given more detail. That's what I get for posting at
1:00AM.

What I want to do us write scripts in CPython that access Windows
ActiveX such as Word and IE. Obviously Jython can't do this (easily at
least). I also have Jython scripts that provide a high level layer on
top of various Java libraries. I'd like to write as much code in Python
as possible. But I have a mixture of incompatible CPython and Jython
scripts - that's why I would like to embed Jython in CPython. If I can
call my Jython scripts from CPython I can have the best of both world's!

Would I be able to embed Jython using JPype? The PyLucene approach
(compile Java source with GCJ then wrap with SWIG) looks interesting -
but complicated.

Here's an example of embedding Jython in a regular Java app:
http://www.jython.org/docs/embedding.html

Imagine doing the same in CPython, but with JPype or GCJ/SWIG.
 
S

Steve Menard

While we are on topic, I am having some trouble understanding JPype
classpath. How do I init the JVM with the folder in which the Python
program is located included in the classpath?

I tried
t = JPackage('.').test

That did not work.

My environment variable includes current folder in the classpath

I tried passing it as an argument to startJVM. Didn't help.

I think my main use is going to be using CPython with a few Java custom
classes and if anyone has a snippet on this it would really help me.
Thanks

That's easy.

First realise that "." denotes the current working directory, not the
"directory where the python program is located".

Second, JPackage uses Java package names.

So, assuming you java classes are properly stord in a directory called
"classes" at the same level as your python script (see how java looks up
classes on the filesystem for what "properly" means), here is a snippet
that will do what you wanted :

#==============================================================
import os, os.path, jpype

root = os.path.abspath(os.path.dirname(__file__))
jpype.startJVM(jpype.getDefaultJVMPath(),
"-Djava.class.path=%s%sclasses" % (root, os.sep))

#==============================================================

Or alternately, if the Java classes you want to use ar in JAR files, in
a lib directory at the same level as your python script :

#==============================================================
import os, os.path, jpype

root = os.path.abspath(os.path.dirname(__file__))
jpype.startJVM(jpype.getDefaultJVMPath(),
"-Djava.ext.dirs=%s%slib" % (root, os.sep))

#==============================================================

The magic in the above script is the __file__ variable, which stores the
absolute path to the currently executing Python script. If you have both
situation (you classes in the classes directory and some JARS containing
stuff that they uses) you can combine the above :

#==============================================================
import os, os.path, jpype

root = os.path.abspath(os.path.dirname(__file__))
jpype.startJVM(jpype.getDefaultJVMPath(),
"-Djava.class.path=%s%sclasses" % (root, os.sep),
"-Djava.ext.dirs=%s%slib" % (root, os.sep))

#==============================================================

Hopefully this will help.
 
S

Steve Menard

Jim said:
Sorry - should have given more detail. That's what I get for posting at
1:00AM.

What I want to do us write scripts in CPython that access Windows
ActiveX such as Word and IE. Obviously Jython can't do this (easily at
least). I also have Jython scripts that provide a high level layer on
top of various Java libraries. I'd like to write as much code in Python
as possible. But I have a mixture of incompatible CPython and Jython
scripts - that's why I would like to embed Jython in CPython. If I can
call my Jython scripts from CPython I can have the best of both world's!

Would I be able to embed Jython using JPype? The PyLucene approach
(compile Java source with GCJ then wrap with SWIG) looks interesting -
but complicated.

Here's an example of embedding Jython in a regular Java app:
http://www.jython.org/docs/embedding.html

Imagine doing the same in CPython, but with JPype or GCJ/SWIG.

Certainly! As far as JPype is concerned though, Jython is just another
library. What I mean is, the API to lauch your Jython scripts will be
identical JPype as though coded in Java. There will be no "magic" to
bridge CPython and Jython.

The obvious advantage of going that way though, instead of the GCJ
approach, is that you keep the full dynamicity of Jython.

I am curious to know what makes your Jython code incompatible with
CPython. If it is only because it uses Java classes, it might not be too
difficult to port them to CPython+Jpype.

Let me know if you need any help.
 
J

Jim Hargrave

I am curious to know what makes your Jython code incompatible with
> CPython. If it is only because it uses Java classes, it might
> not be too
> difficult to port them to CPython+Jpype.

CPython+Jpype may indeed be the way to go in the long run - it's only my
ignorance stoping me at this point :) I'll code some tests and give it
a whirl. Thanks for the help!
 
J

johng2001

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 getDefaultJVMPath() returns None on my system)

import os, os.path
from jpype import *

startJVM("C:/jdk1.5.0/jre/bin/client/jvm.dll",
"-Djava.class.path=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.
 
S

Steve Menard

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 getDefaultJVMPath() returns None on my system)

import os, os.path
from jpype import *

startJVM("C:/jdk1.5.0/jre/bin/client/jvm.dll",
"-Djava.class.path=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.

About the getDefaultJVMPath(), 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").test
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top