Home www.python.org
Download Documentation
Documentation
Overview
Executive Summary
Invoking JPython
JPython Registry
Embedding
Compiling JPython from source
 
Working with Java
The Basics
JavaBean Properties
Java arrays
Subclassing
Building applets, servlets, beans...
 
Python Docs (exits)
Python Tutorial
Library Reference
 
Other
JPython vs. CPython
JPython FAQ
List Archives (exit)
JPython paper (exit)
 
Email Us
jpython@python.org
 
  

Compiling Python Source to Real Java Classes - jpythonc

The previous section describes how Python classes can be created that subclass from Java classes. This works very well when you want to pass a Python class into Java from JPython. This is not adequate for building real Java ".class" files that implement a Java class and can be passed directly to Java programs. This sort of functionality is required to build applets, servlets, or JavaBeans in JPython that can be used exactly like their Java equivalents. It is also very valuable for users who wish to subclass Python classes in Java.

In order to build a real java class from a Python class, you will use the jpythonc tool. A script called "jpythonc" should have been installed in the JPython installation directory when you installed the package. If it was not, it can be invoked as follows: "jpython Tools\jpythonc\jpythonc.py".

JPython 1.1 has a new version of the jpythonc tool, which generates actual Java source code, and then invokes a Java compiler over this source code to create the .class files. Thus, you will need access to a Java compiler in order to use jpythonc. It is recommended that you use Sun's standard javac compiler, or IBM's excellent jikes compiler. Specifically, the Microsoft SDK jvc compiler is not recommended.

Invoke jpythonc like this:

jpythonc [options] [module]*

Options are given in the table below. Note that jpythonc now uses the standard Python getopt module, meaning that both long and short forms of the options are available. The old option names still work, and are described below, but noted as deprecated and will be removed in a future version.

--package package
-p package
-package package
Put all compiled code into the named Java package.
--jar jarfile
-j jarfile
-jar jarfile
Specifies a .jar file to create and put the results of the freeze into. Implies the --deep option.
--deep
-d
-deep
Compile all Python dependencies of the module. This is used for creating applets.
--core
-c
-core
Include the core JPython libraries (about 130K). Needed for applets since Netscape doesn't yet support multiple archives. Implies the --deep option.
--all
-a
-all
Include all of the JPython libraries (everything in core + compiler and parser). Implies the --deep option.
--bean jarfile
-b jarfile
-bean jarfile
Compile into jarfile, including the correct manifest for the bean.
--addpackages pkgs
-A pkgs
-addpackages pkgs
Include Java dependencies from this list of packages. Default is org.python.modules and com.oroinc.text.regex.
--workdir directory
-w directory
-workdir directory
Specify the working directory where the generated Java source code is placed. Default is "./jpywork"
--skip modules
-s modules
-skip modules
Don't include any of these modules in compilation. This is a comma-separated list of modules.
--compiler path
-C path
-compiler path
Use a different compiler than `standard' javac. If this is set to "NONE" then compile ends with the generation of the Java source file. Alternatively, you can set the property python.jpythonc.compiler in the registry.
--compileropts options
-J options
Options passed directly to the Java compiler. Alternatively, you can set the property python.jpythonc.compileropts in the registry.
--falsenames names
-f names
-falsenames names
A comma-separated list of names that are always false. Can be used to short-circuit if clauses.
--help
-h
Print a usage message and exit.
[module]* A list of Python modules to freeze. Can be either modules names that are on the python.path or .py files

The Python module must contain a Python class with the same name as the module, and that Python class must subclass exactly one Java class or interface. The real Java class created will be a subclass of whatever Java class is subclassed.

Examples

On my Windows NT machine, I used the following command to create the file appletdemo.jar (containing all of the applet demos on the web site, as well as the core JPython libraries):

c:\jpython\Demo\applet> jpythonc -core -deep -jar appletdemo.jar *.py

To generate a skeleton class to allow a Python class to be used as java.awt.Component in a Java gui program, I used the following command:

c:\jpython\Demo\javaclasses> jpythonc Graph.py

To generate a JPython-based bean I do the following:

c:\jpython\Demo\bean> jpythonc -deep -bean f2c.jar conversion.FahrenheitToCelsius

To use this with SUN's BDK, I do the following (the important steps are to make the bean and the JPython libraries known to the tool).

  1. modify BDK\beanbox\run.bat (or run.sh) to include the jpython\JavaCode in the CLASSPATH
  2. copy f2c.jar to BDK\jars

You should now be able to access the FahrenheitToCelsius bean from SUN's BeanBox (or other Bean-aware tool).