- ClassDictInit
- __doc__ Strings
- Exceptions
- Parameters
- Importing Jython Modules in Java
- Working with PyObjects
- Writing Jython Classes in Java
- Adding a Java Class as a Built-In Jython Module
- About This Article
Adding a Java Class as a Built-In Jython Module
When you write a Jython module in Java, you also have the option to designate this module as a built-in module. The registry key python.modules.builtin allows you to add modules to the list of built-in modules written in Java. The python.modules.builtin property is a comma-separated list of entries. The entries may appear in three forms. Table 1 shows each form of a module entry and an explanation of its use.
Table 1Built-In Modules Entry Syntax and Description
Entry Syntax |
Description |
name |
Just the name of the Java class. This assumes that the class is in the org.python.modules package. Adding this entry to the python.modules.builtin list allows you to use "import name" in Jython. If you wish to add the class org.python.modules.jnios, the would appear as follows: python.modules.builtin = jnios |
name:class |
This form requires a name and fully qualified class, separated by a colon. The name need not be the class name; it is simply the name Jython uses to refer to this class. If the name duplicates a pre-existing name, the pre-existing module is overridden. To add the class com.mycompany.python.agent as the name use the following: python.modules.builtin = mycompanyagent:org.mycompany.python.agent |
name:null |
This removes the module name from the list of built-in modules. To remove the os module, use the following: python.modules.builtin = os:null |
The python.modules.builtin property is unique in how it is set. Currently setting this property with the D command-line switch does not add a module to this built-in list. It must be set in the registry file, or in the post-properties (second arg) in the initialize method.
To add mymod as a module, compile it, and ensure that it is in the correct directory tree for its package and in the classpath. Then edit the registry file, adding the following:
python.modules.builtin = "mymod:org.python.demo.modules.mymod"
The name, or portion left of the colon, could be any legal identifier, but the class, or right side of the colon, must be a full package.class string uniquely identifying the appropriate class. Once a Java class is added as a built-in, it is available in Jython with just an import of its simple name. The mymod from the preceding example is available with the following:
>>> import mymod
This same mechanism for defining a built-in allows you to substitute a module for an existing built-in, or remove a built-in. All of this contributes to the ease of which Jython is extended and customized with Java.