2.3 Mixing Jython with Java

You can mix Jython and Java codes for CPU extensive tasks, like large loops over primitive data types, reading large files etc.. Such CPU extensive tasks must be implemented in Java. In this case, it is assumed that Jython scripts are used as a glue for various Java libraries with the source files located in directories of the main project directory.

First, build a jar file which contains the byte-codes of Java classes. Again, for this example, set macros/examples/proj as your project directory and select compile and jar project files from the run tool-bar menu. After executing it, a jar file classes.jar appears in the main project directory, which contains the classes of the source files located in the project directory.

Then open a glue Jython script example_java.py. This script calls the class Calc and prints its output. Then execute execute this Jython script by clicking on the run button on the tool bar. You will see the output from the Calc class located in example2/Calc.java.

You can modify the file example2/Calc.java and recompile the all project again using the option javac all files from the run menu. One can also rebuild the jar library. Note that to trigger changes, you will need to reload BeanShell console using the small reload button on the BeanShell tab. Then run the example_java.py again. You should see a different output from the class Calc.