paul@135 | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
paul@135 | 2 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paul@135 | 3 | <head> |
paul@139 | 4 | <title>javaclass/ClassFile</title> |
paul@135 | 5 | <meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> |
paul@135 | 6 | <link xmlns:xlink="http://www.w3.org/1999/xlink" href="styles.css" |
paul@135 | 7 | rel="stylesheet" type="text/css" /> |
paul@135 | 8 | </head> |
paul@135 | 9 | |
paul@135 | 10 | <body> |
paul@139 | 11 | <h1>javaclass</h1> |
paul@139 | 12 | |
paul@139 | 13 | <p>The javaclass collection of packages and utilities (also known as |
paul@139 | 14 | ClassFile) provides a means of importing Java classes and packages directly |
paul@139 | 15 | into Python, without the need for a Java virtual machine, so that the classes |
paul@139 | 16 | may be instantiated, accessed, run and manipulated just like Python classes, |
paul@139 | 17 | and that the resulting objects and methods can be accessed and manipulated |
paul@139 | 18 | just like Python objects and methods.</p> |
paul@135 | 19 | |
paul@139 | 20 | <h2>Quick Examples</h2> |
paul@139 | 21 | |
paul@139 | 22 | <p>It can be quicker to see what this is about by seeing some examples.</p> |
paul@135 | 23 | |
paul@139 | 24 | <h3>The Not Very Convincing Example</h3> |
paul@139 | 25 | |
paul@139 | 26 | <p>You can run Java classes by finding one with a main method and executing |
paul@139 | 27 | it. Here's a comparison of a freshly prepared Java class being run in Python |
paul@139 | 28 | and in a Java virtual machine:</p> |
paul@135 | 29 | |
paul@135 | 30 | <p class="prompt">cd tests/</p> |
paul@135 | 31 | |
paul@135 | 32 | <p class="prompt">javac Value.java</p> |
paul@135 | 33 | |
paul@139 | 34 | <p class="prompt">runclass.py Value</p> |
paul@135 | 35 | |
paul@135 | 36 | <p class="result">v.getValue() correct: 123<br /> |
paul@135 | 37 | v.getValue() correct: 456<br /> |
paul@135 | 38 | v.isPositive() correct: 1<br /> |
paul@135 | 39 | v.isPositive() correct: 0<br /> |
paul@135 | 40 | v.compare(-790) correct: -1<br /> |
paul@135 | 41 | v.compare(-788) correct: 1<br /> |
paul@135 | 42 | v.compare(-789) correct: 0<br /> |
paul@135 | 43 | v.getValue() == v2.getValue() correct: 0<br /> |
paul@135 | 44 | v2.add(-123) correct: 0<br /> |
paul@135 | 45 | v2.getValue() correct: 255</p> |
paul@135 | 46 | |
paul@135 | 47 | <p class="prompt">java Value</p> |
paul@135 | 48 | |
paul@135 | 49 | <p class="result">v.getValue() correct: 123<br /> |
paul@135 | 50 | v.getValue() correct: 456<br /> |
paul@135 | 51 | v.isPositive() correct: true<br /> |
paul@135 | 52 | v.isPositive() correct: false<br /> |
paul@135 | 53 | v.compare(-790) correct: -1<br /> |
paul@135 | 54 | v.compare(-788) correct: 1<br /> |
paul@135 | 55 | v.compare(-789) correct: 0<br /> |
paul@135 | 56 | v.getValue() == v2.getValue() correct: false<br /> |
paul@135 | 57 | v2.add(-123) correct: 0<br /> |
paul@135 | 58 | v2.getValue() correct: 255</p> |
paul@135 | 59 | |
paul@139 | 60 | <h3>The Slightly More Credible Example</h3> |
paul@139 | 61 | |
paul@139 | 62 | <p>It can be more interesting to get into Python's interactive mode and then |
paul@139 | 63 | start playing around with Java classes:</p> |
paul@139 | 64 | |
paul@139 | 65 | <p class="python-result">Python 2.2.2 (#2, Jan 21 2005, 16:16:57)<br /> |
paul@139 | 66 | [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2<br /> |
paul@139 | 67 | Type "help", "copyright", "credits" or "license" for more information.</p> |
paul@139 | 68 | |
paul@139 | 69 | <p class="python-prompt">import javaclass.classhook</p> |
paul@139 | 70 | |
paul@139 | 71 | <p class="python-prompt">from __this__ import Value</p> |
paul@139 | 72 | |
paul@139 | 73 | <p class="python-prompt">dir()</p> |
paul@139 | 74 | |
paul@139 | 75 | <p class="python-result">['Value', '__builtins__', '__doc__', '__name__', |
paul@139 | 76 | 'javaclass']</p> |
paul@139 | 77 | |
paul@139 | 78 | <p class="python-prompt">dir(Value)</p> |
paul@139 | 79 | |
paul@139 | 80 | <p class="python-result">['__class__', '__delattr__', '__dict__', '__doc__', |
paul@139 | 81 | '__getattribute__', '__hash__', '__init__', '__init______I_', '__module__', |
paul@139 | 82 | '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', '__weakref__', |
paul@139 | 83 | 'add', 'add____I_', 'compare', 'compare____I_', 'getClass', 'getClass___', |
paul@139 | 84 | 'getValue', 'getValue___', 'isPositive', 'isPositive___', 'main', |
paul@139 | 85 | 'main___java__lang__String_array_', 'newValue', 'newValue___', 'setValue', |
paul@139 | 86 | 'setValue____I_']</p> |
paul@139 | 87 | |
paul@139 | 88 | <p class="python-prompt">v = Value(20050121)</p> |
paul@139 | 89 | |
paul@139 | 90 | <p class="python-prompt">v.getValue()</p> |
paul@139 | 91 | |
paul@139 | 92 | <p class="python-result">20050121</p> |
paul@139 | 93 | |
paul@139 | 94 | <p class="python-prompt">v.setValue(20050401)</p> |
paul@139 | 95 | |
paul@139 | 96 | <p class="python-prompt">v.getValue()</p> |
paul@139 | 97 | |
paul@139 | 98 | <p class="python-result">20050401</p> |
paul@139 | 99 | |
paul@135 | 100 | <h2>Motivation</h2> |
paul@135 | 101 | |
paul@135 | 102 | <p>Pick one of the following:</p> |
paul@135 | 103 | <ul> |
paul@135 | 104 | <li>The need/desire to access Java libraries from Python without firing up |
paul@135 | 105 | Java virtual machines or switching to Jython (and thereby losing access |
paul@135 | 106 | to various CPython libraries).</li> |
paul@135 | 107 | <li>Static typing for the Python environment, albeit achieved by writing |
paul@135 | 108 | Java or other appropriate languages.</li> |
paul@135 | 109 | <li>Having an open source environment from top to bottom to run Java |
paul@135 | 110 | bytecode on.</li> |
paul@135 | 111 | <li>Experimentation around import hooks, bytecode generation; observation |
paul@135 | 112 | of different runtime and type systems interacting.</li> |
paul@139 | 113 | <li>Using Python libraries from Java.</li> |
paul@139 | 114 | </ul> |
paul@139 | 115 | |
paul@139 | 116 | <h2>Limitations</h2> |
paul@139 | 117 | |
paul@139 | 118 | <p>It isn't all great, however. Here are some reasons why this might not do |
paul@139 | 119 | what you want it to:</p> |
paul@139 | 120 | <ul> |
paul@139 | 121 | <li>It runs on the Python runtime which does not have the security, |
paul@139 | 122 | threading and just-in-time compiler features that people enjoy about Java |
paul@139 | 123 | runtimes, so if what you like to do is to run one big servlet container |
paul@139 | 124 | with lots of classes and threads from different people floating around, |
paul@139 | 125 | this isn't going to protect them from doing all sorts of things to each |
paul@139 | 126 | other and to your system. However, you may take the unfashionable view |
paul@139 | 127 | that the operating system is supposed to do that kind of thing.</li> |
paul@139 | 128 | <li>It works by generating Python bytecode from the Java bytecode in class |
paul@139 | 129 | files (and .jar archives). Generally, anyone who is anyone in the Python |
paul@139 | 130 | pantheon has largely recommended against doing anything with the |
paul@139 | 131 | bytecode, despite noble efforts to make exciting things happen by |
paul@139 | 132 | transforming it, optimising it, and so on - </li> |
paul@135 | 133 | </ul> |
paul@135 | 134 | </body> |
paul@135 | 135 | </html> |