From: Vovan <vov...@ho...> - 2004-08-11 07:20:52
|
Thanks for the explanation. I think I figured that one out. My code last night looked similar to your: I anI = (I)interp.eval("C.C()").__tojava__(I.class); - though I must say this is even cleaner-looking. It's just that I've been using the Python C API for some time, and I expected that it would work similarly here, which it doesn't, but that's okay. :) Thanks, Vovan -----Original Message----- From: Jeff Emanuel [mailto:JEm...@lg...] Sent: Tuesday, August 10, 2004 4:13 AM To: Vovan Subject: RE: [Jython-users] Embedding Python in Java Applications? I didn't read your problem, just your code. How about if your Python class implements a Java interface? I.java: public interface I { public void f(); } C.py: import I class C(I): def f(self): pass Use it from Java like this: PythonInterpreter interp=new PythonInterpreter(); interp.execfile("C.py"); I anI = (I)interp.eval("C.C()").__tojava__(I.class); anI.f(); You can use some indirection to clean up the required syntax in the .py. For example, perhaps you just want the user to define a function f. F.py: def f(): pass You could have a boiler-plate C.py: import I class C2(I): def __init__(self,aCallable): self.otherF=aCallable def f(self): self.otherF() Then in java: PythonInterpreter interp=new PythonInterpreter(); // Do this once. interp.execfile("C.py"); // Do this for each F. interp.execfile("F.py"); I anI = (I)interp.eval("C.C2(f)").__tojava__(I.class); anI.f(); ________________________________ From: Vovan [mailto:vov...@ho...] Sent: Mon 8/9/2004 11:38 AM To: Jeff Emanuel Cc: jyt...@li... Subject: RE: [Jython-users] Embedding Python in Java Applications? So there is no way to get the class wrapped automatically in a nice little object, and I'd have to write a wrapper class myself, which would go through a global interpreter instance? Vovan ________________________________ From: jyt...@li... [mailto:jyt...@li...] On Behalf Of Jeff Emanuel Sent: Monday, August 09, 2004 10:23 AM To: Vovan Cc: jyt...@li... Subject: RE: [Jython-users] Embedding Python in Java Applications? I would do it like this: PythonInterpreter interp=new PythonInterpreter(); interp.exec("import pythontest"); interp.exec("pythontest.testfunc()"); -----Original Message----- From: Vovan [mailto:vov...@ho...] Sent: Friday, August 06, 2004 10:14 AM To: jyt...@li... Subject: [Jython-users] Embedding Python in Java Applications? Hi, This might be a silly question - never used Jython before - but how would I go about using Python modules in a Java application? Well, this warrants a little more explanation. :) So, I am toying around with a simple MUD server. Now, my idea was to use a scripting language to define behaviours of different items, so that I can update those behaviours, and even add new ones, all without ever stopping the server - which is written in Java. Now, I don't know what's the best way to do it - any hints about this are certainly welcome, too :) - but my solution was that I would basically have an Item class, and then I would have a bunch of Python subclasses of item, all overriding a function which gets invoked when the user wants to use the item. But the problem I am having is - I am not sure how to actually now use the Python class? The Java code can detect when the user wants to use the item, but I am not sure how to actually invoke the Python function? I looked around the net, and found this resource here: http://www.samspublishing.com/articles/article.asp?p=26127&seqNum=6 And that says that I need to call __builtin__.__import__, and give it the name of the module I want to import... But it always returns null... I wrote a simple test program to try it out. Basically, the structure is like this: d Project | +-JythonTest.java | +-pythontest.py | +-__init__.py pythontest.py simply contains some functions I could call to see if the module got loaded. And then, inside the Java file, I have: import org.python.core.*; import org.python.util.*; public class JythonTest { public static final void main(String[] agrs) { //Before we can import stuff, we need to initialize the interpreter: PythonInterpreter.initialize(System.getProperties(), null, new String[0]); //Right so first we would like to import the test module... PyModule testModule = (PyModule) __builtin__.__import__("pythontest"); if (testModule == null) { throw new NullPointerException("Object returned by __builtin__.__import__ is null..."); } //Next, let's try and call the testfunc: testModule.invoke("testfunc"); } } Well, what happens when I compile and run that, is that the null pointer exception always gets thrown - the __import__ method returns null... If anyone could tell me what I'm doing wrong here, I'd much obliged. :) TIA, Vovan |