What is jpythonc?

If you really need to embed JPython in a Java application, you have two main choices to choose from. The first option is to use the utility jpythonc to pick a JPython class and generate a Java .classfile that can be called directly from inside your Java code, in a very straightforward away. See the next section of this chapter to learn how to use this utility.

The second option that you have is to import the PythonInterpreterobject class into your Java code. This class allows you to have control of the Python interpreter from Java. The following example demonstrates how the code would be:

importorg.python.util.PythonInterpreter;
importorg.python.core.*;
public class GenNextYear {
public static void main(String []args)
throwsPyException
{
PythonInterpreterinterp = new PythonInterpreter();
System.out.println(“Hello Python World”);
interp.set(“year”, new PyInteger(2000));
interp.exec(“print ‘This is year %d’% (age)”);
interp.exec(“nextyear = year + 1”);
PyObjectnyear = interp.get(“nextyear”);
System.out.println(“Next year is gonna be “+nyear);
}
}

Note that we are able to set/access values to/from the interpreter besides executing commands at the interpreter prompt line.

Check the JavaDoc documentation located at the following address. It is all about org .python .util .Python Interpreter.

Java in a JPython Application

Accessing Java from JPython is no big deal. You can normally work with Java libraries as if you were working with JPython libraries. The process is fully transparent to you. Remember that one of

JPython’s primary goals is to provide easy support to Java libraries.

JPython offers you access to all Java functionality available, which includes

  • Support to JavaBean Properties. The use of JavaBeans is seen by JPython as a solution to simplify the task of talking to most other Java classes.
  • If you need to handle Java Arrays in JPython, you need to use the Jarrayobject. Remember that some Java methods demand argument objects to be in Java array format.

Also good to remember is you can create Python classes that subclass Java classes. This is a helpful option when you need to pass information back and forth between both implementations (Python and Java). Note that you need to create a Python class with the same name of the module that carries class.

The following example shows how a user can instantiate a Java random number class and then interact with that instance:

C:\ jpython>jpython
>>>fromjava.util import Random
>>>number = Random()
>>>number.nextInt()
-857296727
>>> print number.nextDouble()
0.5334538483666526
>>>number.nextInt()
-356857265

Note that we are establishing direct access to the Java library without using any kind of wrappers. The following site is part of the original documentation showing how to use JPython along with Java:

152015024128143245168232148039196038240039088

173205162105045222218070220043007025254056

jpythonc

The JPython distribution provides a tool called jpythoncthat works like a Python compiler for Java. This tool, which is actually just a JPython script, operates by taking aJPython source code (extension .py), and compiling it to real Java class bytecodes, which are executed by the Java Runtime Environment (JRE). Therefore you can write your code in JPython, and later use jpythoncto generate a simple class, a JavaBean, a servlet, or an applet. Note that you need full access to a Java compiler in order to use jpythonc. Internally, the jpythonctool creates a Java source file file, but it needs an external compiler to generate the compiled Java .class file.

Check the installation directory where you installed the JPython package. That’s where the tool is located.

The jpythonctool is very useful for embedding your JPython application in a Java application. After you generate the .class file, you are able to subclass Python classes in Java, and also to create JavaBeans, Servlets, and Applets from a Python class file.

The jpythoncscript accepts several command line options, as listed next. The general format of the command’s syntax is as follows:

jpythonc[options][module]*

The available options are listed as follows. Note that the information provided between parenthesis shows a short way to say the same thing that the long name’s option says.

–package package (-p package)— Puts all compiled code into the named Java package.

–jar jarfile (-j jarfile)— Specifies a .jar file to create and put the results of the freeze into. This option implies the –deep option.

–deep (-d)— Compiles all Python dependencies of the module. This is used for creating applets.

–core (-c)— Includes the core JPython libraries (about 130K). Needed for applets because Netscape doesn’t yet support multiple archives. This option implies the –deep option.

–all (-a)— Includes all the JPython libraries (everything in core + compiler and parser). This option implies the –deep option.

–bean jarfile (-b jarfile)— Compiles into jarfile, including the correct manifest for the bean.

–addpackagespkgs (-A pkgs)— Includes Java dependencies from this list of packages. Default is org.python.modulesand com.oroinc.text.regex.

–workdir directory (-w directory)— Specifies the working directory where the generated Java source code is placed. The default value is ./jpywork.

–skip modules (-s modules)— Doesn’t include any of these modules in compilation. This is a comma-separated list of modules.

–compiler path (-C path)— Uses a compiler different from javac. If this is set to NONE, compiles end with the generation of the Java source file. Alternatively, you can set the property python. jpythonc .compilerin the registry file.

–compileropts options– (-J options)— Passes options directly to the Java compiler. Alternatively, you can set the property python.jpythonc.compileroptsin the registry file.

–falsenames names (-f names)— A comma-separated list of names that are always false. Can be used to short-circuit if clauses.

–help (-h)— Prints a usage message and exits.

[module]*— A list of Python modules to freeze. Can be either module names that are located on the python.pathor .pyfiles.

In order to create an applet, the following syntax is suggested. Note that you need to use the –coreoption in order to include the JPython libraries as part of the applet.

jpythonc -core -deep -jar <appletapp.jar> *.py

In order to create a simple class, the following syntax is suggested:

jpythonc<yourapp.py>

Now, if you just need to create a bean, the following syntax should be used:

jpythonc -deep -bean <filename.jar><beenname>