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