4075
Comment:
|
4779
|
Deletions are marked like this. | Additions are marked like this. |
Line 3: | Line 3: |
[:../: FAQ Home] | JythonFaq <<TableOfContents>> |
Line 7: | Line 10: |
'''How can I use jython classes from my java application?''' | == How can I use jython classes from my java application? == |
Line 9: | Line 12: |
There are several ways to do that. The best way depends on the needs of your application. One possible way is to 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. | There are several ways to do that. The best way depends on the needs of your application. One possible way is to 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 http://jython.org/Project/jythonc.html for details and links that describe using Factories to instantiate python classes at runtime for use with java. Also see item 12 on http://wiki.python.org/jython/JythonFaq/ProgrammingJython''' |
Line 59: | Line 64: |
Creating adapters: | Creating adapters: |
Line 84: | Line 89: |
When running this little application, the jython.jar runtime must be available on the CLASSPATH or specified on the java command line. | When running this little application, the jython.jar runtime must be available on the CLASSPATH or specified on the java command line. |
Line 88: | Line 93: |
'''My modules can not be found when imported from an embedded application''' | == My modules can not be found when imported from an embedded application == |
Line 100: | Line 105: |
PythonInterpreter.initialize(System.getProperties(), props, | PythonInterpreter.initialize(System.getProperties(), props, |
Line 105: | Line 110: |
---- == 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). |
Embedding Jython
Contents
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. One possible way is to 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 http://jython.org/Project/jythonc.html for details and links that describe using Factories to instantiate python classes at runtime for use with java. Also see item 12 on http://wiki.python.org/jython/JythonFaq/ProgrammingJython
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.
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.
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.
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 java class like this:
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:
- A python.path property, if found in the registry file or in the $HOME/.jython file will be used.
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:
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).