See All Titles |
![]() ![]() EmbeddingAs JPython and Java are extremely close to each other, it is not difficult to believe that you can embed JPython code inside a Java application, as well as embed Java code directly into your JPython applications. Both types of implementation are easily supported and coded. By extension, you could create new independent Python interpreters from jpython very easily, as well. JPython in a Java ApplicationIf you really need to embed JPython in a Java application, you have two main choices to choose from. The first option is to use the utility jpythonc to pick a JPython class and generate a Java .class file that can be called directly from inside your Java code, in a very straightforward away. See the next section of this chapter to learn how to use this utility. The second option that you have is to import the PythonInterpreter object class into your Java code. This class allows you to have control of the Python interpreter from Java. The following example demonstrates how the code would be: import org.python.util.PythonInterpreter; import org.python.core.*; public class GenNextYear { public static void main(String []args) throws PyException { PythonInterpreter interp = new PythonInterpreter(); System.out.println("Hello Python World"); interp.set("year", new PyInteger(2000)); interp.exec("print 'This is year %d'% (age)"); interp.exec("nextyear = year + 1"); PyObject nyear = interp.get("nextyear"); System.out.println("Next year is gonna be "+nyear); } } Note that we are able to set/access values to/from the interpreter besides executing commands at the interpreter prompt line. Check the JavaDoc documentation located at the following address. It is all about org.python.util.PythonInterpreter. http://www.jpython.org/docs/api/org.python.util.PythonInterpreter.html Java in a JPython ApplicationAccessing Java from JPython is no big deal. You can normally work with Java libraries as if you were working with JPython libraries. The process is fully transparent to you. Remember that one of JPython's primary goals is to provide easy support to Java libraries. JPython offers you access to all Java functionality available, which includes
Also good to remember is you can create Python classes that subclass Java classes. This is a helpful option when you need to pass information back and forth between both implementations (Python and Java). Note that you need to create a Python class with the same name of the module that carries class. The following example shows how a user can instantiate a Java random number class and then interact with that instance: C:\ jpython>jpython >>> from java.util import Random >>> number = Random() >>> number.nextInt() -857296727 >>> print number.nextDouble() 0.5334538483666526 >>> number.nextInt() -356857265 Note that we are establishing direct access to the Java library without using any kind of wrappers. The following site is part of the original documentation showing how to use JPython along with Java: http://www.jpython.com/docs/usejava.html
|
Index terms contained in this sectionapplicationsJava embedding JPython in embedding JPython Java embedding JPython in JPython embedding programming languages JPython embedding programs Java embedding JPython in software Java embedding JPython in |
© 2002, O'Reilly & Associates, Inc. |