index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Welcome to Py4J &mdash; Py4J</title>
  7. <link rel="stylesheet" href="_static/py4jdoc.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: './',
  12. VERSION: '0.8.1',
  13. COLLAPSE_MODINDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: 'true'
  16. };
  17. </script>
  18. <script type="text/javascript" src="_static/jquery.js"></script>
  19. <script type="text/javascript" src="_static/underscore.js"></script>
  20. <script type="text/javascript" src="_static/doctools.js"></script>
  21. <link rel="author" title="About these documents" href="about.html" />
  22. <link rel="top" title="Py4J" href="#" />
  23. <script type="text/javascript">
  24. var _gaq = _gaq || [];
  25. _gaq.push(['_setAccount', 'UA-18626179-1']);
  26. _gaq.push(['_trackPageview']);
  27. (function() {
  28. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  29. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  30. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  31. })();
  32. </script>
  33. </head>
  34. <body>
  35. <div class="page">
  36. <div class="banner">Py4J - A Bridge between Python and Java</div>
  37. <div class="related">
  38. <h3>Navigation</h3>
  39. <ul>
  40. <li class="right" style="margin-right: 5px">
  41. <a href="genindex.html" title="General Index"
  42. accesskey="I">index</a></li>
  43. <li class="right" >
  44. <a href="py-modindex.html" title="Python Module Index"
  45. >modules</a> |</li>
  46. <li><a href="#">Home</a> |</li>
  47. <li><a href="download.html">Download</a> |</li>
  48. <li><a href="contents.html">Doc</a> |</li>
  49. <li><a href="https://blog.py4j.org">Blog</a> |</li>
  50. <li><a href="https://github.com/bartdag/py4j">github</a> |</li>
  51. <li><a href="about.html">About</a> &raquo;</li>
  52. </ul>
  53. </div>
  54. <div class="sphinxsidebar">
  55. <div class="sphinxsidebarwrapper">
  56. <h3><a href="contents.html">On This Page</a></h3>
  57. <ul>
  58. <li><a class="reference internal" href="#">Welcome to Py4J</a></li>
  59. <li><a class="reference internal" href="#support-resources">Support &amp; Resources</a></li>
  60. <li><a class="reference internal" href="#news">News</a></li>
  61. </ul>
  62. <h3>This Page</h3>
  63. <ul class="this-page-menu">
  64. <li><a href="_sources/index.txt"
  65. rel="nofollow">Show Source</a></li>
  66. </ul>
  67. <div id="searchbox" style="display: none">
  68. <h3>Quick search</h3>
  69. <form class="search" action="search.html" method="get">
  70. <input type="text" name="q" size="18" />
  71. <input type="submit" value="Go" />
  72. <input type="hidden" name="check_keywords" value="yes" />
  73. <input type="hidden" name="area" value="default" />
  74. </form>
  75. <p class="searchtip" style="font-size: 90%">
  76. Enter search terms or a module, class or function name.
  77. </p>
  78. </div>
  79. <script type="text/javascript">$('#searchbox').show(0);</script>
  80. </div>
  81. </div>
  82. <div class="document">
  83. <div class="documentwrapper">
  84. <div class="bodywrapper">
  85. <div class="body">
  86. <div class="section" id="welcome-to-py4j">
  87. <h1>Welcome to Py4J<a class="headerlink" href="#welcome-to-py4j" title="Permalink to this headline">¶</a></h1>
  88. <p>Py4J enables Python programs running in a Python interpreter to dynamically
  89. access Java objects in a Java Virtual Machine. Methods are called as if the
  90. Java objects resided in the Python interpreter and Java collections can be
  91. accessed through standard Python collection methods. Py4J also enables Java
  92. programs to call back Python objects. Py4J is distributed under the <a class="reference external" href="https://github.com/bartdag/py4j/blob/master/LICENSE.txt">BSD
  93. license</a>.</p>
  94. <p>Here is a brief example of what you can do with Py4J. The following Python
  95. program creates a <cite>java.util.Random</cite> instance from a JVM and calls some of its
  96. methods. It also accesses a custom Java class, <cite>AdditionApplication</cite> to add the
  97. generated numbers.</p>
  98. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">py4j.java_gateway</span> <span class="kn">import</span> <span class="n">JavaGateway</span>
  99. <span class="gp">&gt;&gt;&gt; </span><span class="n">gateway</span> <span class="o">=</span> <span class="n">JavaGateway</span><span class="p">()</span> <span class="c"># connect to the JVM</span>
  100. <span class="gp">&gt;&gt;&gt; </span><span class="n">random</span> <span class="o">=</span> <span class="n">gateway</span><span class="o">.</span><span class="n">jvm</span><span class="o">.</span><span class="n">java</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">Random</span><span class="p">()</span> <span class="c"># create a java.util.Random instance</span>
  101. <span class="gp">&gt;&gt;&gt; </span><span class="n">number1</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">nextInt</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c"># call the Random.nextInt method</span>
  102. <span class="gp">&gt;&gt;&gt; </span><span class="n">number2</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">nextInt</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
  103. <span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">number1</span><span class="p">,</span><span class="n">number2</span><span class="p">)</span>
  104. <span class="go">(2, 7)</span>
  105. <span class="gp">&gt;&gt;&gt; </span><span class="n">addition_app</span> <span class="o">=</span> <span class="n">gateway</span><span class="o">.</span><span class="n">entry_point</span> <span class="c"># get the AdditionApplication instance</span>
  106. <span class="gp">&gt;&gt;&gt; </span><span class="n">addition_app</span><span class="o">.</span><span class="n">addition</span><span class="p">(</span><span class="n">number1</span><span class="p">,</span><span class="n">number2</span><span class="p">)</span> <span class="c"># call the addition method</span>
  107. <span class="go">9</span>
  108. </pre></div>
  109. </div>
  110. <p>This is the Java program that was executing at the same time (no code was
  111. generated and no tool was required to run these programs). The
  112. <cite>AdditionApplication app</cite> instance is the <cite>gateway.entry_point</cite> in the
  113. previous code snippet. Note that the Java program must be started before
  114. executing the Python code above. In other words, the Py4J does not start a
  115. JVM.</p>
  116. <div class="highlight-java"><div class="highlight"><pre><span class="kd">public</span> <span class="kd">class</span> <span class="nc">AdditionApplication</span> <span class="o">{</span>
  117. <span class="kd">public</span> <span class="kt">int</span> <span class="nf">addition</span><span class="o">(</span><span class="kt">int</span> <span class="n">first</span><span class="o">,</span> <span class="kt">int</span> <span class="n">second</span><span class="o">)</span> <span class="o">{</span>
  118. <span class="k">return</span> <span class="n">first</span> <span class="o">+</span> <span class="n">second</span><span class="o">;</span>
  119. <span class="o">}</span>
  120. <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
  121. <span class="n">AdditionApplication</span> <span class="n">app</span> <span class="o">=</span> <span class="k">new</span> <span class="n">AdditionApplication</span><span class="o">();</span>
  122. <span class="c1">// app is now the gateway.entry_point</span>
  123. <span class="n">GatewayServer</span> <span class="n">server</span> <span class="o">=</span> <span class="k">new</span> <span class="n">GatewayServer</span><span class="o">(</span><span class="n">app</span><span class="o">);</span>
  124. <span class="n">server</span><span class="o">.</span><span class="na">start</span><span class="o">();</span>
  125. <span class="o">}</span>
  126. <span class="o">}</span>
  127. </pre></div>
  128. </div>
  129. </div>
  130. <div class="section" id="support-resources">
  131. <h1>Support &amp; Resources<a class="headerlink" href="#support-resources" title="Permalink to this headline">¶</a></h1>
  132. <ul class="simple">
  133. <li>Take a look at the tutorial <a class="reference internal" href="getting_started.html"><em>Getting Started with Py4J</em></a>.</li>
  134. <li>Browse the <a class="reference internal" href="contents.html"><em>Py4J Documentation</em></a> or the <a class="reference internal" href="faq.html"><em>Frequently Asked Questions</em></a>.</li>
  135. <li>Ask a question on the <a class="reference external" href="https://groups.google.com/a/py4j.org/forum/#!forum/py4j/join">mailing list</a>.</li>
  136. <li>Look at the <a class="reference external" href="https://github.com/bartdag/py4j/milestones">roadmap</a>.</li>
  137. </ul>
  138. </div>
  139. <div class="section" id="news">
  140. <h1>News<a class="headerlink" href="#news" title="Permalink to this headline">¶</a></h1>
  141. <ul class="simple">
  142. <li><strong>July 25th 2015</strong> - Py4J 0.9 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a>
  143. for more details about the bug fixes. Py4j has moved away from sourcefoge
  144. and is now hosted on <a class="reference external" href="https://www.py4j.org/">py4j.org</a> and it has a new
  145. mailing list.</li>
  146. <li><strong>July 27th 2014</strong> - Py4J 0.8.2 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a>
  147. for more details about the bug fixes.</li>
  148. <li><strong>December 26th 2013</strong> - Py4J 0.8.1 has been released. See the
  149. <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
  150. <li><strong>June 14th 2013</strong> - Py4J 0.8 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a>
  151. for more details about the new features.</li>
  152. <li><strong>June 2nd 2011</strong> - Py4J 0.7 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for
  153. more details about the new features.</li>
  154. <li><strong>February 17th 2011</strong> - Py4J 0.6 has been released. See the
  155. <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
  156. <li><strong>October 30th 2010</strong> - Py4J 0.5 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a>
  157. for more details about the new features.</li>
  158. <li><strong>September 19th 2010</strong> - Py4J 0.4 has been released. See the
  159. <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
  160. <li><strong>April 27th 2010</strong> - Py4J 0.3 has been released. See the <a class="reference internal" href="changelog.html"><em>Changelog</em></a>
  161. for more details about the new features!</li>
  162. <li><strong>February 11th 2010</strong> - Py4J 0.2 has been released. See the
  163. <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about all the new features that found
  164. their way in the latest release!</li>
  165. <li><strong>December 23rd 2009</strong> - Py4J 0.1 has been released. Rejoice!</li>
  166. <li><strong>December 11th 2009</strong> - Py4J is still in the planning phase, but the code
  167. currently works for basic scenarios. A release and a tutorial should be
  168. available in the following weeks.</li>
  169. </ul>
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. <div class="clearer"></div>
  175. </div>
  176. <div class="related">
  177. <h3>Navigation</h3>
  178. <ul>
  179. <li class="right" style="margin-right: 5px">
  180. <a href="genindex.html" title="General Index"
  181. >index</a></li>
  182. <li class="right" >
  183. <a href="py-modindex.html" title="Python Module Index"
  184. >modules</a> |</li>
  185. <li><a href="#">Home</a> |</li>
  186. <li><a href="download.html">Download</a> |</li>
  187. <li><a href="contents.html">Doc</a> |</li>
  188. <li><a href="https://blog.py4j.org">Blog</a> |</li>
  189. <li><a href="https://github.com/bartdag/py4j">github</a> |</li>
  190. <li><a href="about.html">About</a> &raquo;</li>
  191. </ul>
  192. </div>
  193. <div class="footer">
  194. &copy; Copyright 2009-2013, Barthélémy Dagenais.
  195. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.1.
  196. </div>
  197. </div>
  198. </body>
  199. </html>