Differences between revisions 12 and 13
Revision 12 as of 2011-12-22 05:47:39
Size: 4954
Editor: c-67-180-90-205
Comment: Fixed broken link.
Revision 13 as of 2012-11-24 12:03:28
Size: 5179
Editor: host200-79-dynamic
Comment: wiki restore 2013-01-23
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Line 4: Line 5:
JythonFaq [[JythonFaq|JythonFaq]]
Line 9: Line 11:



Line 11: Line 17:
There are several ways to do that. The best way depends on the needs of your application. 
There are several ways to do that. The best way depends on the needs of your application.
Line 15: Line 23:
 * [[http://wiki.python.org/jython/JythonMonthly/Articles/October2006/3|Simple and Efficient Jython Object Factories]] from CharlieGroves
* [[http://wiki.python.org/jython/JythonMonthly/Articles/October2006/3|Simple and Efficient Jython Object Factories]] from [[CharlieGroves|CharlieGroves]]
Line 17: Line 26:
Line 20: Line 30:
Compile the python class into a real java class using the jythonc command. This real java can be used and instances can be created from your application.
Line 22: Line 31:
'''Note: It is no longer recommended that jythonc be used, see ReplaceJythonc''' Compile the python class into a real java class using the jythonc command. This real java can be used and instances can be created from your application.


'''Note: It is no longer recommended that jythonc be used, see [[ReplaceJythonc|ReplaceJythonc]]'''
Line 26: Line 39:
{{{#!java





{{{#!python
Line 33: Line 52:

Line 34: Line 55:





Line 48: Line 75:

Line 49: Line 78:





Line 64: Line 99:

Line 66: Line 103:
Line 67: Line 105:



Line 79: Line 121:
Line 81: Line 124:
The new Foo class can be used from java java class like this:
Line 83: Line 125:
{{{#!java The new Foo class can be used from java class like this:







{{{#!python
Line 95: Line 145:
When compiling the FooTest.java class, the "jpywork" directory should be appended to your classpath.

When compiling the [[FooTest|FooTest]].java class, the "jpywork" directory should be appended to your classpath.
Line 99: Line 152:
Line 100: Line 154:


Line 103: Line 160:
Line 104: Line 162:
Line 108: Line 167:
An application can override the python.path property by calling PythonInterpreter.initialize(..) before any other python code is called:
Line 110: Line 168:
{{{#!java An application can override the python.path property by calling [[PythonInterpreter|PythonInterpreter]].initialize(..) before any other python code is called:







{{{#!python
Line 117: Line 183:

Line 118: Line 186:
Line 121: Line 190:


Line 123: Line 195:
When you run the jython installer, one of the choices is to install as a callable JAR.  Use that selection to create a standalone JAR which can be used to embed.  The jython.jar in the standard install isn't complete to be used as a standalone JAR (despite the fact that they have the same name).
When you run the jython installer, one of the choices is to install as a callable JAR. Use that selection to create a standalone JAR which can be used to embed. The jython.jar in the standard install isn't complete to be used as a standalone JAR (despite the fact that they have the same name).

Embedding Jython

JythonFaq


How can I use jython classes from my java application?

There are several ways to do that. The best way depends on the needs of your application.

Using PythonInterpreter in Factories to instantiate python classes

Using jythonc (deprecated)

Compile the python class into a real java class using the jythonc command. This real java can be used and instances can be created from your application.

Note: It is no longer recommended that jythonc be used, see ReplaceJythonc

Create a python module (say Foo.py) and make a class with the same name as the python module. The class must inherit from a java class or interface. If you don't need a particular java superclass, just use java.lang.Object.

   1   import java
   2   class Foo(java.util.Date):
   3       def toString(self):
   4           return "Foo[" + java.util.Date.toString(self) + "]"

The python class can overwrite all existing methods on the java superclass or interface and these overridden methods can be called from the java application. New methods can by default not be accessed from java. If we add a "bar" method, the method can be used from python, but not from java.

   1   import java
   2   class Foo(java.util.Date):
   3       def __init__(self):
   4           self.count = 0
   5       def bar(self, incr=1):
   6           self.count += incr
   7           return self.count
   8       def toString(self):
   9           cnt = self.bar()
  10           return "Foo[" + java.util.Date.toString(self) + " " + `cnt` + "]"

The jythonc compiler can also create java methods for the python methods, but it need some additional help. This help is specified as a @sig line in the doc-string for the method. A doc-string is added to the example above.

   1   import java
   2   class Foo(java.util.Date):
   3       def __init__(self):
   4           self.count = 0
   5       def bar(self, incr=1):
   6           """@sig void bar(int incr)"""
   7           self.count += incr
   8           return self.count
   9       def toString(self):
  10           cnt = self.bar()
  11           return "Foo[" + java.util.Date.toString(self) + " " + `cnt` + "]"

When this class is compiled with jythonc, A java class Foo.java and Foo.class is created with the java methods toString(), bar() and bar(int incr).

When compiling the Foo.py class, make sure that the Foo actually extends the desired java class. You can check the output from the compilation. It should contain lines like:

  Required packages:
    java.util

  Creating adapters:

  Creating .java files:
    Foo module
      Foo extends java.util.Date

If jython fails to recognize the superclass as a java class, it will silently assume that it is a python class and will not generate the desired java methods.

The new Foo class can be used from java class like this:

   1    public class FooTest {
   2        public static void main(String[] args) {
   3            Foo foo = new Foo();
   4            System.out.println(foo);
   5            foo.bar();
   6            foo.bar(43);
   7            System.out.println(foo);
   8        }
   9    }

When compiling the FooTest.java class, the "jpywork" directory should be appended to your classpath.

When running this little application, the jython.jar runtime must be available on the CLASSPATH or specified on the java command line.


My modules can not be found when imported from an embedded application

The default value for sys.path in an embedded application depend on several things:

  1. A python.path property, if found in the registry file or in the $HOME/.jython file will be used.
  2. The <python.home>/Lib directory is added.

An application can override the python.path property by calling PythonInterpreter.initialize(..) before any other python code is called:

   1     Properties props = new Properties();
   2     props.setProperty("python.path", "/home/modules:scripts");
   3     PythonInterpreter.initialize(System.getProperties(), props,
   4                                  new String[] {""});

The value for python.path must follow the operating system conventions for the PATH environment var (':' separator for unix, ';' for windows)


Library types and functions can't be found by my embedded application

When you run the jython installer, one of the choices is to install as a callable JAR. Use that selection to create a standalone JAR which can be used to embed. The jython.jar in the standard install isn't complete to be used as a standalone JAR (despite the fact that they have the same name).

JythonFaq/EmbeddingJython (last edited 2012-11-24 12:03:28 by host200-79-dynamic)