JythonBook / html / chapter10.html

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>CHAPTER 10: Jython and Java Integration &mdash; Jython Book v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Jython Book v0.1 documentation" href="index.html" />
    <link rel="next" title="Chapter 11: Using Jython in an IDE" href="chapter11.html" />
    <link rel="prev" title="Chapter 9: Input and Output" href="chapter9.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="chapter11.html" title="Chapter 11: Using Jython in an IDE"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="chapter9.html" title="Chapter 9: Input and Output"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Jython Book v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="chapter-10-jython-and-java-integration">
<h1>CHAPTER 10: Jython and Java Integration<a class="headerlink" href="#chapter-10-jython-and-java-integration" title="Permalink to this headline"></a></h1>
<p>Java integration is the heart of Jython application development.  Most Jython developers are either Python developers that are looking to make use of the vast library that the JVM has to offer, or Java developers that would like to utilize the Python language semantics without migrating to a  completely different platform.  The fact is that most Jython developers are using it so that they can take advantage of the vast libraries available to the Java world, and in order to do so there needs to be a certain amount of Java integration in the application.  Whether you plan to use some of the existing Java libraries in your application, or your interested in mixing some great Python code into your Java application, this chapter is geared to help with the integration.</p>
<p>This chapter will focus on integrating Java and Python, but it will take several different vantage points on the topic.  You will learn several techniques to make use Jython code within your Java applications.  Perhaps you’d like to simplify your code a bit, this chapter will show you how to write certain portions of your code in Jython and others in Java so that you can make code as simple as possible.  You’ll learn how to make use of the many Java libraries within your Jython applications using Pythonic syntax!  Forget about coding those programs in Java, why not use Jython so that the Java implementations in the libraries are behind the scenes, this chapter will show how to write in Python and use the libraries directly.</p>
<div class="section" id="using-jython-within-java-applications">
<h2>Using Jython within Java Applications<a class="headerlink" href="#using-jython-within-java-applications" title="Permalink to this headline"></a></h2>
<p>Often times, it is handy to have the ability to make use of Jython from within a Java application.  Perhaps there is a class that would be better implemented in the Python syntax, such as a Javabean.  Or maybe there is a handy piece of Jython code that would be useful within some Java logic.  Whatever the case may be, there are several ways that can be used in order to achieve this combination of technologies.  In this section, we’ll cover some of the older techniques for using Jython within Java, and than go into the current and future best practices for doing this.  In the end, you should have a good understanding for how to use a module, script, or even just a few lines of Jython within your Java application.  You will also have an overall understanding for the way that Jython has evolved in this area.</p>
<div class="section" id="a-bit-of-history">
<h3>A Bit of History<a class="headerlink" href="#a-bit-of-history" title="Permalink to this headline"></a></h3>
<p>Prior to Jython 2.5, the standard distribution of Jython included a utility known as jythonc.  It’s main purpose was to provide the ability to convert Python modules into Java classes so that Java applications could seamlessly make use of Python code, albeit in a roundabout fashion.  jythonc actually compiles the Jython code down into Java .class files and then the classes are utilized within the Java application.  This utility could also be used to freeze code modules, create jar files, and to perform other tasks depending upon which options were used.  This technique is no longer the recommended approach for utilizing Jython within Java applications, but I will briefly cover it over the next couple of paragraphs for good historical reference.  As a matter of fact, jythonc is no longer packaged with the Jython distribution beginning with the 2.5 release.  For those who aren’t interested in the older methodology, please feel free to jump ahead to the next section that covers one of the currently preferred methods for performing such tasks.</p>
<p>In order for jythonc to take a Jython class and turn it into a corresponding Java class, it had to adhere to a few standards.  First, the Jython class had to subclass a Java object, either a class or interface.  It also had to do one of the following: override a Java method, implement a Java method, or create a new method using a signature.  We will go through a brief example utilizing this technique with Jython 2.2.1.  As this is no longer the preferred or accepted approach, we will not delve deep into the topic.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Fish</span><span class="o">.</span><span class="n">py</span>
<span class="k">class</span> <span class="nc">Fish</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Fish Object &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">salt_or_fresh</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">type</span> <span class="o">=</span> <span class="nb">type</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">salt_or_fresh</span> <span class="o">=</span> <span class="n">salt_or_fresh</span>
    <span class="k">def</span> <span class="nf">getType</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;@sig public String getType()&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">type</span>
    <span class="k">def</span> <span class="nf">getSaltOrFresh</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;@sig public String getSaltOrFresh()&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">salt_or_fresh</span>
</pre></div>
</div>
<p>While this method worked well and did what it was meant to do, it  caused a separation between the Jython code and the Java code.  The step of using jythonc to compile Jython into Java is clean, yet, it creates a rift in the development process.   Code should seamlessly work together without a separate compilation procedure.  One should have the ability to seamlessly utilize Jython classes and modules from within a Java application by reference only, and without a special compiler in between.  There have been some significant advances in this area, and I will discuss some of those techniques in the next few sections.  After seeing these next few concepts in action, you will see why jythonc is no longer needed.</p>
</div>
<div class="section" id="object-factories">
<h3>Object Factories<a class="headerlink" href="#object-factories" title="Permalink to this headline"></a></h3>
<p>Perhaps the most widely used technique used today for incorporating Jython code within Java applications is the object factory design pattern.  This idea basically enables seamless integration between Java and Jython via the use of object factories.  There are various different implementations of the logic, but all of them do have the same result in the end.  Implementations of the object factory paradigm allow one to include Jython modules within Java applications without the use of an extra compilation step.  Moreover, this technique allows for a clean integration of Jython and Java code through usage of Java interfaces.  In this section, I will explain the main concept of the object factory technique and then I will show you various implementations.</p>
<p>The key to this design pattern is the creation of a factory method that utilizes PythonInterpreter in order to load the desired Jython module.  Once the factory has loaded the module via PythonInterpreter, it creates a PyObject instance of the module.  Lastly, the factory coerces the PyObject into Java code using the PyObject __tojava__ method.  Overall, the idea is not very difficult to implement and relatively straightforward.  However, the different implementations come into play when it comes to passing references for the Jython module and a corresponding Java interface.   It is important to note that the factory takes care of instantiating the Jython object and translating it into Java.  All work that is performed against the resulting Java object is coded against a corresponding Java interface.  This is a great design because it allows us to change the Jython code implementation if we wish without altering the definition contained within the interface.  The Java code can be compiled once and we can change the Jython code at will without breaking the application.</p>
<p>Let’s take a look at an overview of the entire procedure from a high level.  Say that you’d like to use one of your existing Jython modules as an object container within a Java application.  Begin by coding a Java interface that contains definitions for those methods contained in the module that you’d like to expose to the Java application.  Next, you would modify the Jython module to implement the newly coded Java interface.  After this, code a Java factory class that would make the necessary conversions of the module from a PyObject into a Java object.  Lastly, take the newly created Java object and use it as you wish.  It may sound like a lot of steps in order to perform a simple task, but I think you’ll agree that it is not very difficult once you’ve seen it in action.</p>
<p>Over the next few sections, I will take you through different examples of the implementation.  The first example is a simple and elegant approach that involves a one-to-one Jython object and factory mapping.  In the second example, we’ll take a look at a very loosely coupled approach for working with object factories that basically allows one factory to be used for all Jython objects.  Each of these methodologies has it’s own benefit and you can use the one that works best for you.</p>
<div class="section" id="one-to-one-jython-object-factories">
<h4>One-to-One Jython Object Factories<a class="headerlink" href="#one-to-one-jython-object-factories" title="Permalink to this headline"></a></h4>
<p>We will first discuss the notion of creating a separate object factory for each Jython object we wish to use.  This one-to-one technique can prove to create lots of boilerplate code, but it has some advantages that we’ll take a closer look at later on.  In order to utilize a Jython module using this technique, you must either ensure that the .py module is contained within your sys.path, or hard code the path to the module within your Java code.  Let’s take a look at an example of this technique in use with a Java application that uses a Jython class representing a building.:</p>
<div class="highlight-python"><pre>from org.jython.book.interfaces import BuildingType

class Building(BuildingType):
   def __init__(self, name, address, id):
      self.name = name
      self.address  =  address
      self.id = id

   def getBuildingName(self):
      return self.name

   def getBuildingAddress(self):
      return self.address

   def getBuldingId(self):
      return self.id


package org.jython.book.interfaces;

public interface BuildingType {

    public String getBuildingName();
    public String getBuildingAddress();
    public String getBuildingId();

}


package org.jython.book.util;

import org.jython.book.interfaces.BuildingType;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class BuildingFactory {

    private PyObject buildingClass;

    public BuildingFactory() {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from Building import Building");
        buildingClass = interpreter.get("Building");
    }

    public BuildingType create(String name, String location, String id) {
        PyObject buildingObject = buildingClass.__call__(new PyString(name),
new PyString(location),
new PyString(id));
        return (BuildingType)buildingObject.__tojava__(BuildingType.class);
    }

}

package org.jython.book;

import org.jython.book.util.BuildingFactory;
import org.jython.book.interfaces.BuildingType;

public class Main {

    private static void print(BuildingType building) {
        System.out.println("Building Info: " +
                building.getBuildingId() + " " +
                building.getBuildingName() + " " +
                building.getBuildingAddress());

    }

    public static void main(String[] args) {
        BuildingFactory factory = new BuildingFactory();
        print(factory.create("BUILDING-A", "100 WEST MAIN", "1"));
        print(factory.create("BUILDING-B", "110 WEST MAIN", "2"));
        print(factory.create("BUILDING-C", "120 WEST MAIN", "3"));
    }
}</pre>
</div>
<p>Let’s perform a play-by-play analysis of what goes on in this code.  We begin with a Jython module named Building.py that is placed somewhere on our sys.path.  Now, we must first ensure that there are no name conflicts before doing so or we could see some quite unexpected results.  It is usually a safe bet to place this file at the source root for your application unless you explicitly place the file in your sys.path elsewhere.  You can see that our Building.py object is a simple container for holding building information.   We must explicitly implement a Java interface within our Jython class.  This will allow the PythonInterpreter to coerce our object later.  Our second piece of code is the Java interface that we implemented in Building.py.  As you can see from the code, the returning Jython types are going to be coerced into Java types, so we define our interface methods using the eventual Java types.</p>
<p>The third piece of code in the example above plays the most important role in the game, this is the object factory that will coerce our Jython code into a resulting Java class.  In the constructor, a new instance of the PythonInterpreter is created which we then utilize the interpreter to obtain a reference to our Jython object and stores it into our PyObject.  Next, there is a static method named create that will be called in order to coerce our Jython object into Java and return the resulting class.  It does so by actually performing a __call__ on the PyObject wrapper itself, and as you can see we have the ability to pass parameters to it if we like.  The parameters must also be wrapped by PyObjects.  The coercion takes place when the __tojava__ method is called on the PyObject wrapper.  In order to make object implement our Java interface, we must pass the interface EmployeeType.class to the __tojava__ call.</p>
<p>The last bit of provided code, Main.java, shows how to make use of our factory.  You can see that the factory takes care of all the heavy lifting and our implementation in Main.java is quite small.  Simply call the factory.create() method to instantiate a new PyObject and coerce it into Java.</p>
<p>This procedure for using the object factory design has the benefit of maintaining complete awareness of the Jython object from within Java code.  In other words, creating a separate factory for each Jython object allows for the use of passing arguments into the constructor of the Jython object.  Since the factory is being designed for a specific Jython object, we can code the __call__ on the PyObject with specific arguments that will be passed into the new constructor of the coerced Jython object.  Not only does this allow for passing arguments into the constructor, but also increases the potential for good documentation of the object since the Java developer will know exactly what the new constructor will look like.  Although in most cases the developer using this technique will be the person writing the Jython objects as well.  The procedures performed in this subsection are probably the most frequently used throughout the Jython community.  In the next section, we’ll take a look at the same technique applied to a generic object factory that can be used by any Jython object.</p>
</div>
<div class="section" id="making-use-of-a-loosely-coupled-object-factory">
<h4>Making Use of a Loosely Coupled Object Factory<a class="headerlink" href="#making-use-of-a-loosely-coupled-object-factory" title="Permalink to this headline"></a></h4>
<p>The object factory design does not have to be implemented using a one to one strategy such as that depicted in the example above.  It is possible to design the factory in such a way that it is generic enough to be utilized for any Jython object.  This technique allows for less boilerplate coding as you only require one Singleton factory that can be used for all Jython objects, and it also allows for ease of use as you can separate the object factory logic into it’s own project and then apply it wherever you’d like.  For instance, I’ve created a project in my environment that basically contains a Jython object factory that can be used in any Java application in order to create Jython objects from Java without worrying about the factory.   You can create a similar project or use the one that I’ve created and linked to the source for this book.  In this section we’ll take a look at the design behind this project and how it works.</p>
<p>Let’s take a look at the same example from above and apply the loosely coupled object factory design.  You will notice that this technique forces the Java developer to do a bit more work when creating the object from the factory, but it has the advantage of saving the time that is spent to create a separate factory for each Jython object.  You can also see that now we need to code setters into our Jython object and expose them via the Java interface as we can no longer make use of the constructor for passing arguments into the object since the loosely coupled factory makes a generic __call__ on the PyObject.:</p>
<div class="highlight-python"><pre>from org.jython.book.interfaces import BuildingType

class Building(BuildingType):
    def __init__(self):
       self.name = None
       self.address  =  None
       self.id = -1

    def getBuildingName(self):
        return self.name

    def setBuildingName(self, name):
        self.name = name;

    def getBuildingAddress(self):
        return self.address

    def setBuildingAddress(self, address):
        self.address = address

    def getBuildingId(self):
        return self.id

    def setBuildingId(self, id):
        self.id = id
package org.jython.book.interfaces;

public interface BuildingType {

    public String getBuildingName();
    public String getBuildingAddress();
    public int getBuildingId();
    public void setBuildingName(String name);
    public void setBuildingAddress(String address);
    public void setBuildingId(int id);

}



import java.util.logging.Level;
import java.util.logging.Logger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class JythonObjectFactory {
   private static JythonObjectFactory instance = null;
   private static PyObject pyObject = null;

   protected JythonObjectFactory() {

   }

   public static JythonObjectFactory getInstance(){
        if(instance == null){
            instance = new JythonObjectFactory();
        }

        return instance;

    }


   public static Object createObject(Object interfaceType, String moduleName){
       Object javaInt = null;
       PythonInterpreter interpreter = new PythonInterpreter();
       interpreter.exec("from " + moduleName + " import " + moduleName);

       pyObject = interpreter.get(moduleName);

        try {

            PyObject newObj = pyObject.__call__();

            javaInt = newObj.__tojava__(Class.forName(interfaceType.toString().substring(
                    interfaceType.toString().indexOf(" ")+1, interfaceType.toString().length())));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JythonObjectFactory.class.getName()).log(Level.SEVERE, null, ex);
        }

        return javaInt;
   }

}

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jythonbook.interfaces.BuildingType;
import org.jybhonbook.factory.JythonObjectFactory;

public class Main {

    public static void main(String[] args) {

        JythonObjectFactory factory = JythonObjectFactory.getInstance();

        BuildingType building = (BuildingType) factory.createObject(
                BuildingType.class, "Building");
        building.setBuildingName("BUIDING-A");
        building.setBuildingAddress("100 MAIN ST.");
        building.setBuildingId(1);
        System.out.println(building.getBuildingId() + " " + building.getBuildingName() + " " +
                building.getBuildingAddress());

    }

}</pre>
</div>
<p>If we follow this paradigm then you can see that our Jython module must be coded a bit differently than it was in our one-to-one example, but not much.   The main differences are in the initializer as it no longer takes any arguments, and we also now have coded setter methods into our object.  The rest of the concept still holds true in that we must implement a Java interface that will expose those methods we wish to invoke from within our Java application.  In this case, we coded the BuildingType.java interface and included the necessary setter definitions so that we have a way to load our class with values.</p>
<p>Our next step is to either code a loosely coupled object.  If you take a look at the code in the JythonObjectFactory.java class you will see that it is a singleton; that is it can only be instantiated one time.  The important method to look at is createObject() as it does all of the work.  As you can see from the code, the PythonInterpreter is responsible for obtaining a reference to the Jython object name that we pass as a String value into the method.  Once the PythonInterpreter has obtained the object and stored it into a PyObject, it’s __call__() method is invoked without any parameters.  This will retrieve an empty object that is then stored into another PyObject that is referenced by newObj.  Lastly, our newly obtained object is coerced into Java code by calling the __tojava__() method which takes the fully qualified name of the Java interface we’ve implemented with our Jython object.  The new Java object is then returned.</p>
<p>Taking a look at the Main.java code, you can see that the factory is instantiated or referenced via the use of the JythonObjectFactory.getInstance().  Once we have an instance of the factory, the createObject(Interface, String) is called passing the interface and a string representation of the module name we wish to use.    The code must cast the coerced object using the interface as well.  This example assumes that the object resides somewhere on your sys.path, otherwise you can use the createObjectFromPath(Interface, String) that accepts the string representation for the path to the module we’d like to coerce.  This is of course not a preferred technique since it will now include hard-coded paths,  but it can be useful to use this technique for testing purposes.  Say you’ve got two Jython modules coded and one of them contains a different object implementation for testing purposes, this technique will allow you to point to the test module.</p>
<p>Another similar, yet, more refined implementation omits the use of PythonInterpreter and instead makes use of PySystemState.  Why would we want another implementation that produces the same results?  Well, there are a couple of reasons worth noting.  The loosely-coupled object factory design I described in the beginning of this section instantiates the PythonInterpreter and then makes calls against it.  This can cause a decrease in performance, as it is quite expensive to use the interpreter.  On the other hand, we can make use of PySystemState and save ourselves the trouble of incurring extra overhead making calls to the interpreter.  Not only does the next example show how to utilize this technique, but it also shows how we can make calls upon the coerced object and pass arguments at the same time.  This is a limitation of the object factory we showed previously.:</p>
<div class="highlight-python"><pre>JythonObjectFactory.java

package org.jython.book.util;

import org.python.core.Py;

import org.python.core.PyObject;
import org.python.core.PySystemState;

public class JythonObjectFactory {

    private final Class interfaceType;
    private final PyObject klass;

    // likely want to reuse PySystemState in some clever fashion since expensive to setup...
    public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
        this.interfaceType = interfaceType;
        PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
        PyObject module = importer.__call__(Py.newString(moduleName));
        klass = module.__getattr__(className);
        System.err.println("module=" + module + ",class=" + klass);
    }

    public JythonObjectFactory(Class interfaceType, String moduleName, String className) {
        this(new PySystemState(), interfaceType, moduleName, className);
    }

    public Object createObject() {
        return klass.__call__().__tojava__(interfaceType);
    }

    public Object createObject(Object arg1) {
         return klass.__call__(Py.java2py(arg1)).__tojava__(interfaceType);
    }

    public Object createObject(Object arg1, Object arg2) {
         return klass.__call__(Py.java2py(arg1), Py.java2py(arg2)).__tojava__(interfaceType);
    }

    public Object createObject(Object arg1, Object arg2, Object arg3) {
         return klass.__call__(Py.java2py(arg1), Py.java2py(arg2), Py.java2py(arg3)).__tojava__(interfaceType);
    }

    public Object createObject(Object args[], String keywords[]) {
        PyObject convertedArgs[] = new PyObject[args.length];
        for (int i = 0; i &lt; args.length; i++) {
            convertedArgs[i] = Py.java2py(args[i]);
        }
        return klass.__call__(convertedArgs, keywords).__tojava__(interfaceType);
    }

    public Object createObject(Object... args) {
        return createObject(args, Py.NoKeywords);
    }

}

import org.jython.book.interfaces.BuildingType;
import org.jython.book.util.JythonObjectFactory;

public class Main{

 public static void main(String args[]) {
        // what other control options should we provide to the factory?
        // jsr223 might have some good ideas, but also let's keep some simple code usage
        // for now, let's just try out and refine
        JythonObjectFactory factory = new JythonObjectFactory(
                BuildingType.class, "building", "Building");

        BuildingType building = (BuildingType) factory.createObject();

        building.setBuildingName("BUIDING-A");
        building.setBuildingAddress("100 MAIN ST.");
        building.setBuildingId(1);

        System.out.println(building.getBuildingId() + " " + building.getBuildingName() + " " +
                building.getBuildingAddress());
    }

}</pre>
</div>
<p>As you can see from the code above, it has quite a few differences from the object factory implementation shown previously.  First, you can see that the instantiation of the object factory requires some different arguments.  In this case, we pass in the interface, module, and class name.  Next, you can see that the PySystemState obtains a reference to the importer PyObject.  The importer then makes a __call__ to the module we’ve requested.  By the way, the requested module must be contained somewhere on the sys.path.    Lastly, we obtain a reference to our class by calling the __getattr__ method on the module.  We can now use the returned class to perform the coercion of our Jython object into Java.  As mentioned previously, you’ll note that this particular implementation includes several createObject() variations allowing one to pass arguments to the module when it is being called.  This, in effect, gives us the ability to pass arguments into the initializer of the  Jython object.</p>
<p>Which object factory is best?  Your choice, depending upon the situation you’re application is encountering.  Bottom line is that there are several ways to perform the object factory design and they all allow seamless use of Jython objects from within Java code.</p>
<p>Now that we have a coerced Jython object, we can go ahead and utilize the methods that have been defined in the Java interface.  As you can see, the simple example above sets a few values and then prints the object values out.  Hopefully you can see how easy it is to create a single object factory that can be used for any Jython object rather than just one.</p>
</div>
<div class="section" id="returning-doc-strings">
<h4>Returning __doc__ Strings<a class="headerlink" href="#returning-doc-strings" title="Permalink to this headline"></a></h4>
<p>It is also very easy to obtain the __doc__ string from any of your Jython classes by coding an accessor method on the object itself.  We’ll add some code to the building object that was used in the previous examples.  It doesn’t matter what type of factory you decide to work with, this trick will work with both.:</p>
<div class="highlight-python"><pre>from org.jython.book.interfaces import BuildingType

class Building(BuildingType):
    ''' Class to hold building objects '''

    def __init__(self):
       self.name = None
       self.address  =  None
       self.id = -1

    def getBuildingName(self):
        return self.name

    def setBuildingName(self, name):
        self.name = name;

    def getBuildingAddress(self):
        return self.address

    def setBuildingAddress(self, address):
        self.address = address

    def getBuildingId(self):
        return self.id

    def setBuildingId(self, id):
        self.id = id

    def getDoc(self):
        return self.__doc__

package org.jython.book.interfaces;

public interface BuildingType {

    public String getBuildingName();
    public String getBuildingAddress();
    public int getBuildingId();
    public void setBuildingName(String name);
    public void setBuildingAddress(String address);
    public void setBuildingId(int id);
    public String getDoc();

}


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jython.book.interfaces.BuildingType;
import org.plyjy.factory.JythonObjectFactory;

public class Main {

    public static void main(String[] args) {

        JythonObjectFactory factory = JythonObjectFactory.getInstance();
        BuildingType building = (BuildingType) factory.createObject(
                BuildingType.class, "Building");
        building.setBuildingName("BUIDING-A");
        building.setBuildingAddress("100 MAIN ST.");
        building.setBuildingId(1);
        System.out.println(building.getBuildingId() + " " + building.getBuildingName() + " " +
                building.getBuildingAddress());
        System.out.println(building.getDoc());

   }
}

1 BUIDING-A 100 MAIN ST.
 Class to hold building objects</pre>
</div>
</div>
<div class="section" id="applying-the-design-to-different-object-types">
<h4>Applying the Design to Different Object Types<a class="headerlink" href="#applying-the-design-to-different-object-types" title="Permalink to this headline"></a></h4>
<p>This design will work with all object types, not just plain old Jython objects.   In the following example, the Jython module is a class containing a simple calculator method.  The factory coercion works the same way, the result is a Jython class that is converted into Java.:</p>
<div class="highlight-python"><pre>from org.jython.book.interfaces import CostCalculatorType

class CostCalculator(CostCalculatorType, object):
    ''' Cost Calculator Utility '''

    def __init__(self):
        print 'Initializing'
        pass

    def calculateCost(self, salePrice, tax):
        return salePrice + (salePrice * tax)

package org.jython.book.interfaces;

public interface CostCalculatorType {

    public double calculateCost(double salePrice, double tax);

}

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.plyjy.factory.JythonObjectFactory;

public class Main {

    public static void main(String[] args) {

        JythonObjectFactory factory = JythonObjectFactory.getInstance();
        CostCalculatorType costCalc = (CostCalculatorType) factory.createObject(
                CostCalculatorType.class, "CostCalculator");
        System.out.println(costCalc.calculateCost(25.96, .07));

    }
}

Initializing
27.7772</pre>
</div>
<p>We can also use Python properties in our Jython objects along with Java.  As a matter of fact, the Java interface is coded with the same definitions as it would be for a regular accessor-style object.  In this example, we’ll populate a Jython object that contains automobile properties.  Although the automobile object in this example is not very life-like as it does not contain make, model, year, etc. for various legal reasons, I think you’ll get the idea.:</p>
<div class="highlight-python"><pre>from org.jython.book.interfaces import AutomobileType

class Automobile(AutomobileType, object):
    ''' Bean to hold automobile objects '''

    def __init__(self):
        print 'Initializing Now'
        pass

    def setType(self, value):
        self.__type = value
        print 'setting object: ' + value

    def getType(self):
        print 'getting object'
        return self.__type

    type = property(fget=getType,
                    fset=setType,
                    doc="The Type of the Automobile.")

    def setColor(self, value):
        self.__color = value

    def getColor(self):
        return self.__color

    color = property(fget=getColor,
                     fset=setColor,
                     doc="Color of Automobile.")

    def getDoc(self):
        return self.__doc__

package org.jython.book.interfaces;

public interface AutomobileType {
    public String getType();
    public void setType(String value);
    public String getColor();
    public void setColor(String value);
    public String getDoc();


}

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jython.book.interfaces.AutomobileType;
import org.plyjy.factory.JythonObjectFactory;


public class Main {

    private static void print(AutomobileType auto) {
        System.out.println("Doc: " + auto.getDoc());
        System.out.println("Type: " + auto.getType());
        System.out.println("Color: " + auto.getColor());

    }

    public static void main(String[] args) {

        JythonObjectFactory factory = JythonObjectFactory.getInstance();
        AutomobileType automobile = (AutomobileType) factory.createObject(
                AutomobileType.class, "Automobile");
        automobile.setType("Sport");
        automobile.setColor("red");
        print (automobile);

    }

}
Initializing Now
setting object: Sport
Doc:  Bean to hold automobile objects
getting object
Type: Sport
Color: red</pre>
</div>
</div>
</div>
<div class="section" id="jsr-223">
<h3>JSR-223<a class="headerlink" href="#jsr-223" title="Permalink to this headline"></a></h3>
<p>Along with the release of Java SE 6 came a new advantage for dynamic languages on the JVM.  JSR-223 enables dynamic languages to be callable via Java in a seamless manner.  Although this method of accessing Jython code is not quite as flexible as using an object factory, it is quite useful for running short Jython scripts from within Java code.  The scripting project (<a class="reference external" href="https://scripting.dev.java.net/">https://scripting.dev.java.net/</a>) contains many engines that can be used to run different languages within Java.  In order to run the Jython engine, you must obtain jython-engine.jar from the scripting project and place it into your classpath.  You must also place jython.jar in the classpath, and it does not yet function with Jython 2.5 so Jython 2.2.1 must be used.</p>
<p>Below is a small example showing the utilization of the scripting engine.</p>
<div class="highlight-python"><pre>import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {

    public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
        engine.eval("import sys");
        engine.eval("print sys");
        engine.put("a", 42);
        engine.eval("print a");
        engine.eval("x = 2 + 2");
        Object x = engine.get("x");
        System.out.println("x: " + x);
    }

}

*sys-package-mgr*: processing new jar, '/jsr223-engines/jython/build/jython-engine.jar'
*sys-package-mgr*: processing modified jar, '/System/Library/Java/Extensions/QTJava.zip'
sys module
42
x: 4</pre>
</div>
</div>
<div class="section" id="utilizing-pythoninterpreter">
<h3>Utilizing PythonInterpreter<a class="headerlink" href="#utilizing-pythoninterpreter" title="Permalink to this headline"></a></h3>
<p>A similar technique to JSR-223 for embedding Jython is making use of the PythonInterpreter directly.  This style of embedding code is very similar to making use of a scripting engine, but it has the advantage of working with Jython 2.5.  Another advantage is that the PythonInterpreter enables you to make use of PyObjects directly.  In order to make use of the PythonInterpreter technique, you only need to have jython.jar in your classpath, there is no need to have an extra engine involved.:</p>
<div class="highlight-python"><pre>import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Main {
    /**
     * @param args the command line arguments
     */
     public static void main(String[] args) throws PyException {
        PythonInterpreter interp = new PythonInterpreter();
        interp.exec("import sys");
        interp.exec("print sys");
        interp.set("a", new PyInteger(42));
        interp.exec("print a");
        interp.exec("x = 2+2");
        PyObject x = interp.get("x");
        System.out.println("x: " + x);
    }
}
&lt;module 'sys' (built-in)&gt;
42
x: 4</pre>
</div>
</div>
</div>
<div class="section" id="using-java-within-jython-applications">
<h2>Using Java within Jython Applications<a class="headerlink" href="#using-java-within-jython-applications" title="Permalink to this headline"></a></h2>
<p>Making use of Java from within Jython applications is about as seamless as using external Jython modules within a Jython script.  You simply import the required Java classes and use them directly.  Java classes can be called in the same fashion as Jython classes, and the same goes for method calling.  You simply call a class method and pass parameters the same way you’d do in Python.</p>
<p>Type coercion occurs much as it does when using Jython in Java in order to seamlessly integrate the two languages.  In the following table, you will see the Java types that are coerced into Python types and how they match up.  This table was taken from the Jython user guide.</p>
<table border="1" class="docutils">
<colgroup>
<col width="28%" />
<col width="9%" />
<col width="7%" />
<col width="11%" />
<col width="46%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Java Type</th>
<th class="head">&nbsp;</th>
<th class="head">&nbsp;</th>
<th class="head">Python Type</th>
<th class="head">&nbsp;</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>char</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>String(length of 1)</td>
</tr>
<tr><td>boolean</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>Integer(true = not zero)</td>
</tr>
<tr><td>byte, short, int, long</td>
<td>&nbsp;</td>
<td>Integer</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td>java.lang.String, byte[], char[]</td>
<td>String</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td>java.lang.Class</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>JavaClass</td>
<td>&nbsp;</td>
</tr>
<tr><td>Foo[]</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>Array(containing objects of  class or subclass of Foo)</td>
</tr>
<tr><td>java.lang.Object</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>String</td>
<td>&nbsp;</td>
</tr>
<tr><td>orb.python.core.PyObject</td>
<td>Unchanged</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr><td>Foo</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>JavaInstance representing Java class Foo</td>
</tr>
</tbody>
</table>
<p>Table 10.1- Python and Java Types</p>
<p>Another thing to note about the utilization of Java within Jython is that there may be some naming conflicts that arise.  If a Java object conflicts with a Python object name, then you can simply fully qualify the Java object in order to ensure that the conflict is resolved.</p>
<p>In the next couple of examples, you will see some Java objects being imported and used from within Jython.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">java.lang</span> <span class="kn">import</span> <span class="n">Math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Math</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="mf">4</span><span class="p">,</span> <span class="mf">7</span><span class="p">)</span>
<span class="go">7L</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Math</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mf">10</span><span class="p">,</span><span class="mf">5</span><span class="p">)</span>
<span class="go">100000.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Math</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mf">8.75</span><span class="p">)</span>
<span class="go">9L</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Math</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="mf">9.765</span><span class="p">)</span>
<span class="go">9.765</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Math</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="o">-</span><span class="mf">9.765</span><span class="p">)</span>
<span class="go">9.765</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">java.lang</span> <span class="kn">import</span> <span class="n">System</span> <span class="k">as</span> <span class="n">javasystem</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">javasystem</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="p">(</span><span class="s">&quot;Hello&quot;</span><span class="p">)</span>
<span class="go">Hello</span>
</pre></div>
</div>
<p>Now let’s create a Java object and use it from within a Jython application.:</p>
<div class="highlight-python"><pre>public class Beach {

    private String name;
    private String city;
    private String state;
    private boolean publicBeach;

    public Beach(String name, String city, String state, boolean publicBeach){
        this.name = name;
        this.city = city;
        this.state = state;
        this.publicBeach = publicBeach;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isPublicBeach() {
        return publicBeach;
    }

    public void setPublicBeach(boolean publicBeach) {
        this.publicBeach = publicBeach;
    }

}

&gt;&gt;&gt; import Beach
&gt;&gt;&gt; beach = Beach("Cocoa Beach","Cocoa Beach","FL",True)
&gt;&gt;&gt; beach.getName()
u'Cocoa Beach'
&gt;&gt;&gt; print beach.getName()
Cocoa Beach
&gt;&gt;&gt; print beach.isPublicBeach()
True</pre>
</div>
<p>One thing you’ll need to do is ensure that the Java class you wish to use resides within your CLASSPATH.  In the example above, I created a JAR file that contained the Beach class and then put that JAR on the CLASSPATH.</p>
<p>It is also possible to extend Java classes via Jython classes.  This allows us to extend the functionality of a given Java class using Jython objects, which can be quite helpful at times.  The next example shows a Jython class extending a Java class that includes some calculation functionality.  The Jython  class then adds another calculation method and makes use of the calculation methods from both the Java class and the Jython class.:</p>
<div class="highlight-python"><pre>public class Calculator {

    public Calculator(){

    }

    public double calculateTip(double cost, double tipPercentage){
        return cost * tipPercentage;
    }

    public double calculateTax(double cost, double taxPercentage){
        return cost * taxPercentage;
    }

}

import Calculator
from java.lang import Math

class JythonCalc(Calculator):
    def __init__(self):
        pass

    def calculateTotal(self, cost, tip_and_tax):
        return cost + tip_and_tax




if __name__ == "__main__":
    calc = JythonCalc()
    cost = 23.75
    tip = .15
    tax = .07
    print "Starting Cost: ", cost
    print "Tip Percentage: ", tip
    print "Tax Percentage: ", tax
    print Math.round(calc.calculateTotal(cost,
               (calc.calculateTip(cost, .15) + calc.calculateTax(cost, .07))))

Starting Cost:  23.75
Tip Percentage:  0.15
Tax Percentage:  0.07
29</pre>
</div>
</div>
<div class="section" id="conclusion">
<h2>Conclusion<a class="headerlink" href="#conclusion" title="Permalink to this headline"></a></h2>
<p>Integrating Jython and Java is really at the heart of the language.  Using Java within Jython works just as adding other Jython modules, a very seamless integration.  What makes this nice is that now we can utilize the full set of libraries and APIs available to Java from our Jython applications.  Having the ability of using Java within Jython also provides the advantage of writing Java code in the Python syntax…something we all enjoy.</p>
<p>Utilizing design patterns such as the Jython object factory, we can also harness our Jython code from within Java applications.  Although jythonc is no longer part of the Jython distribution, we can still effectively use Jython from within Java.  There are many examples of the object factory available, as well as projects such as PlyJy (<a class="reference external" href="http://kenai.com/projects/plyjy">http://kenai.com/projects/plyjy</a>) that give the ability to use object factories by simply including a JAR in your Java application.</p>
<p>There are more ways to use Jython from within Java as well.  The Java language added scripting language support with JSR-223 with the release of Java 6.  Using a jython engine, we can make use of the JSR-223 dialect to sprinkle Jython code into our Java applications.  Similarly, the PythonInterpreter can be used from within Java code to invoke Jython.  Also keep an eye on projects such as Clamp (<a class="reference external" href="http://github.com/groves/clamp/tree/master">http://github.com/groves/clamp/tree/master</a>) that are on the horizon.  The Clamp project has the goal to make use of annotations in order to create Java classes from Jython classes.  It will be exciting to see where this project goes, and it will be documented once the project has been completed.</p>
<p>In the next chapter, you will see how we can use Jython within some integrated development environments.  Specifically, we will take a look at developing Jython with Eclipse and Netbeans.  Utilizing an IDE can greatly increase developer productivity, and also assist in subtleties such as adding modules and JAR files to the classpath.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">CHAPTER 10: Jython and Java Integration</a><ul>
<li><a class="reference external" href="#using-jython-within-java-applications">Using Jython within Java Applications</a><ul>
<li><a class="reference external" href="#a-bit-of-history">A Bit of History</a></li>
<li><a class="reference external" href="#object-factories">Object Factories</a><ul>
<li><a class="reference external" href="#one-to-one-jython-object-factories">One-to-One Jython Object Factories</a></li>
<li><a class="reference external" href="#making-use-of-a-loosely-coupled-object-factory">Making Use of a Loosely Coupled Object Factory</a></li>
<li><a class="reference external" href="#returning-doc-strings">Returning __doc__ Strings</a></li>
<li><a class="reference external" href="#applying-the-design-to-different-object-types">Applying the Design to Different Object Types</a></li>
</ul>
</li>
<li><a class="reference external" href="#jsr-223">JSR-223</a></li>
<li><a class="reference external" href="#utilizing-pythoninterpreter">Utilizing PythonInterpreter</a></li>
</ul>
</li>
<li><a class="reference external" href="#using-java-within-jython-applications">Using Java within Jython Applications</a></li>
<li><a class="reference external" href="#conclusion">Conclusion</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="chapter9.html"
                                  title="previous chapter">Chapter 9:  Input and Output</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="chapter11.html"
                                  title="next chapter">Chapter 11:  Using Jython in an IDE</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/chapter10.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="chapter11.html" title="Chapter 11: Using Jython in an IDE"
             >next</a> |</li>
        <li class="right" >
          <a href="chapter9.html" title="Chapter 9: Input and Output"
             >previous</a> |</li>
        <li><a href="index.html">Jython Book v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Josh Juneau, Frank Wierzbicki, Jim Baker, Leo Soto, ViVictor Ng.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.