File: usejava.html

package info (click to toggle)
jython 2.2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,708 kB
  • ctags: 46,200
  • sloc: python: 150,937; java: 86,267; xml: 1,080; perl: 104; sh: 93; makefile: 81; ansic: 24
file content (399 lines) | stat: -rw-r--r-- 11,819 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- THIS PAGE IS AUTOMATICALLY GENERATED.  DO NOT EDIT. -->
<!-- Mon Oct 22 14:35:40 2007 -->
<!-- USING HT2HTML 2.0 -->
<!-- SEE http://ht2html.sf.net -->
<!-- User-specified headers:
Title: Accessing Java from Jython

-->

<head>
<title>Accessing Java from Jython</title>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="generator" content="HT2HTML/2.0">
<style type="text/css">
body { margin: 0px; }
</style>
</head>
<body bgcolor="#ffffff" text="#000000"
      marginwidth="0" marginheight="0"
      link="#0000bb"  vlink="#00000"
      alink="#ff0000">
<!-- start of page table -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<!-- start of banner row -->
<tr>
<!-- start of corner cells -->
<td width="150" valign="middle" bgcolor="#cccccc" class="corner">

<center>
    <a href="http://www.jython.org/">
    <img border="0" src="./images/jython-new-small.gif"></a></center> </td>
<td width="15" bgcolor="#cccccc">&nbsp;&nbsp;</td><!--spacer-->
<!-- end of corner cells -->
<!-- start of banner -->
<td width="90%" bgcolor="#cccccc" class="banner">
<!-- start of site links table -->
<table width="100%" border="0"
cellspacing="0" cellpadding="2"
       bgcolor="#ffffff">
<tr>
    <td bgcolor="#cccccc">
<a href="http://www.jython.org/">Home</a>
    </td>
    <td bgcolor="#cccccc">
<a href="http://www.python.org/">www.python.org</a>
    </td>
</tr><tr>
    <td bgcolor="#cccccc">
<a href="http://www.jython.org/download.html">Download</a>
    </td>
    <td bgcolor="#cccccc">
<b>Documentation</b>
    </td>
</tr>
</table><!-- end of site links table -->

</td><!-- end of banner -->
</tr><!-- end of banner row -->
<tr><!-- start of sidebar/body row -->
<!-- start of sidebar cells -->
<td width="150" valign="top" bgcolor="#cccccc" class="sidebar">
<!-- start of sidebar table -->
<table width="100%" border="0" cellspacing="0" cellpadding="3"
       bgcolor="#ffffff">
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Documentation
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<a href="./index.html">Overview</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="whatis.html">Executive Summary</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="interpreter.html">Invoking Jython</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="registry.html">Jython Registry</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="embedding.html">Embedding</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="compile.html">Compiling Jython from source</a>
</td></tr>
<tr><td bgcolor="#cccccc">&nbsp;
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Working with Java
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<b>The Basics</b>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="properties.html">JavaBean Properties</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="jarray.html">Java arrays</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="subclassing.html">Subclassing</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="jythonc.html">Building applets, servlets, beans...</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="jreload.html">Reloading java classes</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="zxjdbc.html">zxJDBC</a>
</td></tr>
<tr><td bgcolor="#cccccc">&nbsp;
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Python Docs (exits)
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.python.org/doc/tut/tut.html">Python Tutorial</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.python.org/doc/lib/lib.html">Library Reference</a>
</td></tr>
<tr><td bgcolor="#cccccc">&nbsp;
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Other
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<a href="differences.html">Jython vs. CPython</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.jython.org/cgi-bin/faqw.py?req=index">Jython FAQ (exit)</a>
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.geocrawler.com/archives/3/7017/2000/">List Archives</a> (exit)
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.python.org/workshops/1997-10/proceedings/hugunin.html">JPython paper</a> (exit)
</td></tr>
<tr><td bgcolor="#cccccc">&nbsp;
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Contact
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://lists.sourceforge.net/lists/listinfo/jython-users">Questions on Jython?<br>jython-users</a>
</td></tr>
<tr><td bgcolor="#cccccc">
&nbsp;
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://www.python.org/">
<center>
    <img border="0" src="./images/PythonPoweredSmall.gif"></center>
</a>
</td></tr>
<tr><td bgcolor="#cccccc">
&nbsp;
</td></tr>
<tr><td bgcolor="#cccccc">
<a href="http://sourceforge.net/">
<center>
 <img src="http://sourceforge.net/sflogo.php?group_id=12867" width="88" height="31" border="0" alt="SourceForge Logo"></center>
</a>
</td></tr>
</table><!-- end of sidebar table -->

</td>
<td width="15">&nbsp;&nbsp;</td><!--spacer-->
<!-- end of sidebar cell -->
<!-- start of body cell -->
<td valign="top" width="90%" class="body"><br>
<h3>Accessing Java from Jython</h3>

<P>One of the goals of Jython is to make it as simple as possible to
use existing Java libraries from Python.

<H3>Example</H3>

<P>The following example of an interactive session with Jython shows
how a user could create an instance of the Java random number class
(found in java.util.Random) and then interact with that instance.</P>

<blockquote><PRE>
C:\jython&gt;jython
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from java.util import Random
&gt;&gt;&gt; r = Random()
&gt;&gt;&gt; r.nextInt()
-790940041
&gt;&gt;&gt; for i in range(5):
...&nbsp;&nbsp;&nbsp;&nbsp; print r.nextDouble()
...
0.23347681506123852
0.8526595592189546
0.3647833839988137
0.3384865260567278
0.5514469740469587
&gt;&gt;&gt;
</PRE></blockquote>

<H3>More Details</H3>

<P>Hopefully, this example should make it clear that there are very
few differences between using Java packages and using Python packages
when working under Jython.  There are a few things to keep in mind.

<H4>Importing</H4>

<p>
<blockquote><pre>
Jython 2.0 on java1.2.1
Type "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from java.util import *
&gt;&gt;&gt; Random
&lt;jclass java.util.Random at 31702169&gt;
&gt;&gt;&gt; Hashtable
&lt;jclass java.util.Hashtable at 7538094&gt;
&gt;&gt;&gt; 
</pre></blockquote>

<H4>Creating Class Instances</H4>

<P>You can create an instance of a Java class exactly the way you
would create an instance of a Python class.  You must &quot;call&quot;
the class with a set of arguments that is appropriate for one of the
Java class's constructors.  See the section below for more details on
what constitutes appropriate arguments.

<H4>Calling Java Methods and Functions</H4>

<P>Java classes have both static and instance methods this makes them
behave much like a cross between a Python module and class.  As a
user, you should rarely need to be concerned with this difference.</P>

<P>Java methods and functions are called just exactly like their
Python counterparts.  There is some automatic type coercion that goes
on both for the types being passed in and for the value returned by
the method.  The following table shows how Python objects are coerced
to Java objects when passed as arguments in a function call.  The
<em>Java Types</em> show the expected Java type for the argument,
and the <em>Allowed Python Types</em>
shows what Python objects can be converted to the given
Java type.  Notice the special behavior of <tt>String</tt>'s when a
<tt>java.lang.Object</tt> is expected.  This behavior might change if
it is shown to cause problems.
  
<p><TABLE BORDER="1" WIDTH="100%">
    <TR>
	<TH>Java Types</TH>
	<TH>Allowed Python Types</TH>
    </TR>
    <TR>
	<TD>char</TD>
	<TD>String (must have length 1) </TD>
    </TR>
    <TR>
	<TD>boolean</TD>
	<TD>Integer (true = nonzero) </TD>
    </TR>
    <TR>
	<TD>byte, short, int, long</TD>
	<TD>Integer</TD>
    </TR>
    <TR>
	<TD>float, double</TD>
	<TD>Float</TD>
    </TR>
    <TR>
	<TD>java.lang.String,<br>byte[], char[]</TD>
	<TD>String </TD>
    </TR>
    <TR>
	<TD>java.lang.Class</TD>
	<TD>Class or JavaClass (only if class subclasses from exactly
	    one Java class; mutiple inheritance from more than one
	    Java class is now illegal)</TD>
    </TR>
    <TR>
	<TD>Foo[]</TD>
	<TD>Array (must contain objects of class or subclass of Foo)</TD>
    </TR>
    <TR>
	<TD>java.lang.Object</TD>
	<TD>String-&gt;java.lang.String, all others unchanged </TD>
    </TR>
    <TR>
	<TD>org.python.core.PyObject</TD>
	<TD>All unchanged </TD>
    </TR>
    <TR>
	<TD>Foo</TD>
	<TD>Instance-&gt;Foo (if Instance is subclass of Foo);  <BR>
	    JavaInstance -&gt; Foo (if JavaInstance is instance of Foo
	    or subclass) </TD>
    </TR>
</TABLE>

<p>Returned values from a Java method are also possibly coerced back to
an object that is more readily usable in Python.  The following table
shows those coercions.
  
<p><TABLE BORDER="1" WIDTH="100%">
    <TR>
	<TH>Java Type</TH>
	<TH>Returned Python Type</TH>
    </TR>
    <TR>
	<TD>char</TD>
	<TD>String (of length 1) </TD>
    </TR>
    <TR>
	<TD>boolean</TD>
	<TD>Integer (true = 1, false = 0) </TD>
    </TR>
    <TR>
	<TD>byte, short, int, long</TD>
	<TD>Integer</TD>
    </TR>
    <TR>
	<TD>float, double</TD>
	<TD>Float</TD>
    </TR>
    <TR>
	<TD>java.lang.String</TD>
	<TD>String </TD>
    </TR>
    <TR>
	<TD>java.lang.Class</TD>
	<TD>JavaClass which represents given Java class</TD>
    </TR>
    <TR>
	<TD>Foo[]</TD>
	<TD>Array (containing objects of class or subclass of Foo)</TD>
    </TR>
    <TR>
	<TD>org.python.core.PyObject (or subclass)</TD>
	<TD>Unchanged </TD>
    </TR>
    <TR>
	<TD>Foo</TD>
	<TD>JavaInstance which represents the Java Class Foo</TD>
    </TR>
</TABLE>
 
<H4>Overloaded Java Method Signatures</H4>

<P>Java methods are allowed to be overloaded for different signatures
(types and number of arguments). When different versions of the method
differ in the number of arguments that they expect, the appropriate
method can be easily determined from the number of arguments passed to
the method.</P>

<P>When the difference is instead in the types of the arguments, more
work is required.  The possible signatures are sorted in a consistent
order that should ensure the appropriate method is chosen first.
<b>TBD: documentation this order!</b>

<P>If you need to call a Java method with a particular signature and
this is not happening in the easy way, you can use the following
workaround:</P>

<P>Assume that foo has two methods, &quot;void foo(int x); void
foo(byte x);&quot;.  To call the second method you could write the
following:</P>

<blockquote><PRE>
from java.lang import Byte
foo(Byte(10))
</PRE></blockquote>

<P>I'm not convinced that any better solution to this problem is possible.

<H4>Naming Conflicts with Python Keywords</H4>

<P>Because Java has a different set of keywords than Python, there are
many Java classes that have method and function names that conflict
with Python's keyword set.  Where the intent can be unambiguously
determined, no identifier mangling is necessary, such as when keywords 
are used as attributes on objects.  Thus you can naturally write:

<blockquote><PRE>
java.lang.System.out.print("hi")
</PRE></blockquote>

or

<blockquote><PRE>
java.lang.Runtime.getRuntime().exec(cmd)</tt>
</PRE></blockquote>

<p>In the rare case where the conflict can't be resolved due to Python's
grammar, you should modify the reserved word by appended an underscore 
to the end of it, e.g. <tt>print_</tt>

<p>
</td><!-- end of body cell -->
</tr><!-- end of sidebar/body row -->
</table><!-- end of page table -->
</body></html>