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
|
<!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: Using the Jython compiler - jythonc
-->
<head>
<title>Using the Jython compiler - jythonc</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"> </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">
<tr><td bgcolor="#666699"><b><font color="#ffffff">
Working with Java
</font></b></td></tr>
<tr><td bgcolor="#cccccc">
<a href="usejava.html">The Basics</a>
</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">
<b>Building applets, servlets, beans...</b>
</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">
<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">
<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">
<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">
</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">
</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"> </td><!--spacer-->
<!-- end of sidebar cell -->
<!-- start of body cell -->
<td valign="top" width="90%" class="body"><br>
<h3>Compiling Python Source to Real Java Classes</h3>
<P>The previous section describes how Python classes can be created
that subclass from Java classes. This works very well when you want
to pass a Python class into Java from Jython. This is not adequate
for building real Java ".class" files that implement a Java class and
can be passed directly to Java programs. This sort of functionality
is required to build applets, servlets, or JavaBeans in Jython that
can be used exactly like their Java equivalents. It is also very
valuable for users who wish to subclass Python classes in Java.</P>
<P>In order to build a real java class from a Python class, you will
use the <tt>jythonc</tt> tool. A script called "jythonc" should
have been installed in the Jython installation directory when you
installed the package. If it was not, it can be invoked as follows:
"jython Tools\jythonc\jythonc.py".</P>
<p>The jythonc tool generates actual Java source code, and then
invokes a Java compiler over this source code to create the .class
files. Thus, you will need access to a Java compiler in order to use
jythonc.
It is recommended that you use Sun's standard <tt>javac</tt> compiler,
or IBM's excellent <tt>jikes</tt> compiler.
Specifically, the Microsoft SDK <tt>jvc</tt> compiler is not recommended.
<P>Invoke jythonc like this:
<P><B>jythonc [options] [module]*</b>
<p>Options are given in the table below. Note that jythonc now uses
the standard Python <tt>getopt</tt> module, meaning that both long and
short forms of the options are available. The old option names still
work, and are described below, but noted as
<font size=-1 color="blue">deprecated</font> and will be removed in a
future version.
<p><TABLE BORDER="1">
<TR>
<td><b>--package</b> <em>package</em>
<br><b>-p</b> <em>package</em>
<br><font size=-1 color="blue"><b>-package</b> <em>package</em></font>
</td>
<td>Put all compiled code into the named Java package.
</tr><tr>
<TD><B>--jar</B> <em>jarfile</em>
<br><b>-j</b> <em>jarfile</em>
<br><font size=-1 color="blue"><b>-jar</b> <em>jarfile</em></font>
</td>
<TD>Specifies a .jar file to create and put the results of the
freeze into. Implies the <b>--deep</b> option.
</tr><tr>
<TD><B>--deep</B>
<br><b>-d</b>
<br><font size=-1 color="blue"><b>-deep</b></font>
</td>
<TD>Compile all Python dependencies of the module. This is used
for creating applets.</TD>
</TR><TR>
<TD><B>--core</B>
<br><b>-c</b>
<br><font size=-1 color="blue"><b>-core</b></font>
</td><TD>Include the core Jython libraries (about 130K). Needed for
applets since Netscape doesn't yet support multiple archives.
Implies the <b>--deep</b> option.
</TR><TR>
<TD><b>--all</B>
<br><b>-a</b>
<br><font size=-1 color="blue"><b>-all</b></font>
</td><TD>Include all of the Jython libraries (everything in core +
compiler and parser). Implies the <b>--deep</b> option.
</TR><TR>
<TD><b>--bean</b> <em>jarfile</em>
<br><b>-b</b> <em>jarfile</em>
<br><font size=-1 color="blue"><b>-bean</b> <em>jarfile</em></font>
</td><TD>Compile into <em>jarfile</em>, including the correct manifest for
the bean.
</TR><TR>
<td><b>--addpackages <em>pkgs</em>
<br>-A <em>pkgs</em>
<br><font size=-1 color="blue"><b>-addpackages</b>
<em>pkgs</em></font>
</td><td>Include Java dependencies from this list of packages.
Default is <tt>org.python.modules</tt> and
<tt>org.apache.oro.text.regex</tt>.
</TR><TR>
<TD><B>--workdir</B> <em>directory</em>
<br><B>-w</B> <em>directory</em>
<br><font size=-1 color="blue"><b>-workdir</b>
<em>directory</em></font>
</td><TD>Specify the working directory where the generated Java
source code is placed. Default is "./jpywork"
</tr><tr>
<td><b>--skip</b> <em>modules</em>
<br><b>-s</b> <em>modules</em>
<br><font size=-1 color="blue"><b>-skip</b> <em>modules</em></font>
</td><td>Don't include any of these modules in compilation. This
is a comma-separated list of modules.
</tr><tr>
<td><b>--compiler</b> <em>path</em>
<br><b>-C</b> <em>path</em>
<br><font size=-1 color="blue"><b>-compiler</b> <em>path</em></font>
</td><td>Use a different compiler than `standard' javac. If this
is set to "NONE" then compile ends with the generation of the
Java source file. Alternatively, you can set the property
<tt>python.jythonc.compiler</tt> in the
<a href="registry.html">registry</a>.
</tr><tr>
<td><b>--compileropts</b> <em>options</em>
<br><b>-J</b> <em>options</em>
</td><td>Options passed directly to the Java compiler.
Alternatively, you can set the property
<tt>python.jythonc.compileropts</tt> in the
<a href="registry.html">registry</a>.
</tr><tr>
<td><b>--falsenames</b> <em>names</em>
<br><b>-f</b> <em>names</em>
<br><font size=-1 color="blue"><b>-falsenames</b> <em>names</em></font>
</td><td>A comma-separated list of names that are always false. Can
be used to short-circuit if clauses.
</tr><tr>
<td><b>--help</b>
<br><b>-h</b>
</td><td>Print a usage message and exit.
</TR><TR>
<TD>[module]*</TD>
<TD>A list of Python modules to freeze. Can be either modules
names that are on the <tt>python.path</tt> or .py files</TD>
</TR>
</TABLE>
<p>In order to create real java class, the Python module must contain
a Python class with the same name as the module, and that Python class
must subclass exactly one Java class or interface.
The real Java class created will be a subclass of whatever Java class
is subclassed.
<p>
<h3>Compiling python methods to java methods</h3>
Normally, compiled python methods are not directly callable from
java. There is simple not sufficient information in a python method
definition to create a useable java method. There are however two
ways a python method be made into real java method. Both ways require
that the python class have a java class or a java interface as its
base classes.
<ul>
<li><b>Overriding / Implementing a java method.</b>
<p>By overriding a java method, the overridden method's signature
is used for the real java class. That includes return type,
privacy modifier, arguments, argument types and throws clauses.
<p><li><b>Adding java method signature to python methods docstrings.</b>
<p>Using a special convention in the method docstring
allow for the creation of a corresponding java method.
The signature must be written on one line and must start with the
text "@sig". All class names (except those ion java.lang) must
be specified as fully qualified class names. Example:
<blockquote><pre>
def setExpression(self, e):
"@sig public void setExpression(java.lang.String e)"
</pre></blockquote>
</ul>
<h3>Examples</h3>
<P>On my Windows NT machine, I used the following command to create
the file appletdemo.jar (containing all of the applet demos on the web
site, as well as the core Jython libraries):</P>
<PRE>c:\jython\Demo\applet> jythonc --core --deep --jar appletdemo.jar *.py</PRE>
<P><em>Note:</em> One special thing to consider when building applets is the java VM
version used when compiling the sources must usually be the same as
the VM version used by the browser. If a newer JVM is used when compiling
the applets, the compiled code may have references to classes that does
not exists in older JVM's.
<P>To generate a skeleton class to allow a Python class to be used as
java.awt.Component in a Java gui program, I used the following
command:</P>
<PRE>c:\jython\Demo\javaclasses> jythonc Graph.py</PRE>
<P>To generate a Jython-based bean I do the following:</P>
<PRE>c:\jython\Demo\bean> jythonc --deep --bean f2c.jar conversion.FahrenheitToCelsius</PRE>
<P>To use this with SUN's BDK, I do the following (the important steps
are to make the bean and the Jython libraries known to the tool).</P>
<ol>
<li>modify BDK\beanbox\run.bat (or run.sh) to include the
jython\JavaCode in the CLASSPATH
<li>copy f2c.jar to BDK\jars
</ol>
<P>You should now be able to access the FahrenheitToCelsius bean from
SUN's BeanBox (or other Bean-aware tool).
<p>
<h3>Freezing modules</h3>
jythonc can also be used to freeze a python application. The frozen
application can then be distributed and deployed as any other java
application.
<p>Some diffrences between an interpreted application and a frozen
application exists:
<ul>
<li>Properties are diffrent. XXX
<li>sys.argv[0]
<li>loading of python classes.
</ul>
<h3>Examples</h3>
XXX
<p>
</td><!-- end of body cell -->
</tr><!-- end of sidebar/body row -->
</table><!-- end of page table -->
</body></html>
|