File: embedding.ht

package info (click to toggle)
jython 2.2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,708 kB
  • ctags: 46,200
  • sloc: python: 150,937; java: 86,267; xml: 1,080; perl: 104; sh: 93; makefile: 81; ansic: 24
file content (42 lines) | stat: -rw-r--r-- 1,107 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Title: Embedding Jython

<h3>Embedding Jython</h3>

<P>There are several options for embedding Jython in a Java
application.  Sometimes the nicest approach is to make a real Java
class out of a Python class and then just use that Python class from
Java code.  The simplest approach to embedding Jython is to use the
PythonInterpreter object.</P>

<P>JavaDoc documentation for 
<A HREF="javadoc/org/python/util/PythonInterpreter.html">
org.python.util.PythonInterpreter</A>
<BR>
The following example is distributed under Demo/embed/SimpleEmbedded.java

<hr>
<pre>
import org.python.util.PythonInterpreter; 
import org.python.core.*; 

public class SimpleEmbedded { 
    public static void main(String []args)
	throws PyException
    { 
	PythonInterpreter interp =
	    new PythonInterpreter();

	System.out.println("Hello, brave new world");
	interp.exec("import sys");
	interp.exec("print sys");

	interp.set("a", new PyInteger(42));
	interp.exec("print a");
	interp.exec("x = 2+2");
	PyObject x = interp.get("x");

	System.out.println("x: "+x);
	System.out.println("Goodbye, cruel world");
    }
}
</pre>