From: Kent J. <ke...@td...> - 2004-08-10 13:02:15
|
Vovan, Here are some options for using Jython classes from Java. 1. Use jythonc - this is a compiler that creates true .class files from your Jython classes. Classes created with jythonc can be used the same way as classes created in Java. I personally have had problems with jythonc and don't use it, but many people seem to have success with this approach. You will not be able to create and modify classes dynamically though. 2. If a Jython class implements a Java interface, then instances of the class can be freely used from Java. This approach might work well for you, if you can create an interface which describes behaviours. This approach still leaves the question of how to create the Jython object. Probably the best way for you to do this would be with a PythonInterpreter. Take a look at org.python.util.PyServlet for an example of how to do this. PyServlet uses an interpreter to get a Class object defined in Jython, then it uses the Class to create an actual servlet instance. You could also write some Jython code that creates instances of all the behaviors you need and puts them into the local namespace. You could run this code with an interpreter and extract the instances from the interpreter namespace. 3. You might also want to consider writing your main application in Jython, making use of Java libraries as needed. Then any Jython instances needed by the Java code can be created in the Jython application and just passed to the Java objects that need them. This is my preferred approach. (Jython classes to be used by Java still need to implement a Java interface.) Kent > > From: "Vovan" <vov...@ho...> > Date: 2004/08/09 Mon PM 01:38:31 EDT > To: "'Jeff Emanuel'" <JEm...@lg...> > 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 > <http://www.samspublishing.com/articles/article.asp?p=26127&seqNum=6> > &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 > > > > > |