| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>Welcome to Py4J — Py4J</title>
- <link rel="stylesheet" href="_static/py4jdoc.css" type="text/css" />
- <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: './',
- VERSION: '0.8.1',
- COLLAPSE_MODINDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: 'true'
- };
- </script>
- <script type="text/javascript" src="_static/jquery.js"></script>
- <script type="text/javascript" src="_static/underscore.js"></script>
- <script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="author" title="About these documents" href="about.html" />
- <link rel="top" title="Py4J" href="#" />
- <script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', 'UA-18626179-1']);
- _gaq.push(['_trackPageview']);
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>
- </head>
- <body>
- <div class="page">
- <div class="banner">Py4J - A Bridge between Python and Java</div>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 5px">
- <a href="genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="#">Home</a> |</li>
- <li><a href="download.html">Download</a> |</li>
- <li><a href="contents.html">Doc</a> |</li>
- <li><a href="https://blog.py4j.org">Blog</a> |</li>
- <li><a href="https://github.com/bartdag/py4j">github</a> |</li>
- <li><a href="about.html">About</a> »</li>
-
- </ul>
- </div>
- <div class="sphinxsidebar">
- <div class="sphinxsidebarwrapper">
- <h3><a href="contents.html">On This Page</a></h3>
- <ul>
- <li><a class="reference internal" href="#">Welcome to Py4J</a></li>
- <li><a class="reference internal" href="#support-resources">Support & Resources</a></li>
- <li><a class="reference internal" href="#news">News</a></li>
- </ul>
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="_sources/index.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
- <div id="searchbox" style="display: none">
- <h3>Quick search</h3>
- <form class="search" action="search.html" method="get">
- <input type="text" name="q" size="18" />
- <input type="submit" value="Go" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- <p class="searchtip" style="font-size: 90%">
- Enter search terms or a module, class or function name.
- </p>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body">
-
- <div class="section" id="welcome-to-py4j">
- <h1>Welcome to Py4J<a class="headerlink" href="#welcome-to-py4j" title="Permalink to this headline">¶</a></h1>
- <p>Py4J enables Python programs running in a Python interpreter to dynamically
- access Java objects in a Java Virtual Machine. Methods are called as if the
- Java objects resided in the Python interpreter and Java collections can be
- accessed through standard Python collection methods. Py4J also enables Java
- 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
- license</a>.</p>
- <p>Here is a brief example of what you can do with Py4J. The following Python
- program creates a <cite>java.util.Random</cite> instance from a JVM and calls some of its
- methods. It also accesses a custom Java class, <cite>AdditionApplication</cite> to add the
- generated numbers.</p>
- <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">py4j.java_gateway</span> <span class="kn">import</span> <span class="n">JavaGateway</span>
- <span class="gp">>>> </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>
- <span class="gp">>>> </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>
- <span class="gp">>>> </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>
- <span class="gp">>>> </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>
- <span class="gp">>>> </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>
- <span class="go">(2, 7)</span>
- <span class="gp">>>> </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>
- <span class="gp">>>> </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>
- <span class="go">9</span>
- </pre></div>
- </div>
- <p>This is the Java program that was executing at the same time (no code was
- generated and no tool was required to run these programs). The
- <cite>AdditionApplication app</cite> instance is the <cite>gateway.entry_point</cite> in the
- previous code snippet. Note that the Java program must be started before
- executing the Python code above. In other words, the Py4J does not start a
- JVM.</p>
- <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>
- <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>
- <span class="k">return</span> <span class="n">first</span> <span class="o">+</span> <span class="n">second</span><span class="o">;</span>
- <span class="o">}</span>
- <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>
- <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>
- <span class="c1">// app is now the gateway.entry_point</span>
- <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>
- <span class="n">server</span><span class="o">.</span><span class="na">start</span><span class="o">();</span>
- <span class="o">}</span>
- <span class="o">}</span>
- </pre></div>
- </div>
- </div>
- <div class="section" id="support-resources">
- <h1>Support & Resources<a class="headerlink" href="#support-resources" title="Permalink to this headline">¶</a></h1>
- <ul class="simple">
- <li>Take a look at the tutorial <a class="reference internal" href="getting_started.html"><em>Getting Started with Py4J</em></a>.</li>
- <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>
- <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>
- <li>Look at the <a class="reference external" href="https://github.com/bartdag/py4j/milestones">roadmap</a>.</li>
- </ul>
- </div>
- <div class="section" id="news">
- <h1>News<a class="headerlink" href="#news" title="Permalink to this headline">¶</a></h1>
- <ul class="simple">
- <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>
- for more details about the bug fixes. Py4j has moved away from sourcefoge
- and is now hosted on <a class="reference external" href="https://www.py4j.org/">py4j.org</a> and it has a new
- mailing list.</li>
- <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>
- for more details about the bug fixes.</li>
- <li><strong>December 26th 2013</strong> - Py4J 0.8.1 has been released. See the
- <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
- <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>
- for more details about the new features.</li>
- <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
- more details about the new features.</li>
- <li><strong>February 17th 2011</strong> - Py4J 0.6 has been released. See the
- <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
- <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>
- for more details about the new features.</li>
- <li><strong>September 19th 2010</strong> - Py4J 0.4 has been released. See the
- <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about the new features.</li>
- <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>
- for more details about the new features!</li>
- <li><strong>February 11th 2010</strong> - Py4J 0.2 has been released. See the
- <a class="reference internal" href="changelog.html"><em>Changelog</em></a> for more details about all the new features that found
- their way in the latest release!</li>
- <li><strong>December 23rd 2009</strong> - Py4J 0.1 has been released. Rejoice!</li>
- <li><strong>December 11th 2009</strong> - Py4J is still in the planning phase, but the code
- currently works for basic scenarios. A release and a tutorial should be
- available in the following weeks.</li>
- </ul>
- </div>
- </div>
-
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 5px">
- <a href="genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><a href="#">Home</a> |</li>
- <li><a href="download.html">Download</a> |</li>
- <li><a href="contents.html">Doc</a> |</li>
- <li><a href="https://blog.py4j.org">Blog</a> |</li>
- <li><a href="https://github.com/bartdag/py4j">github</a> |</li>
- <li><a href="about.html">About</a> »</li>
-
- </ul>
- </div>
- <div class="footer">
- © Copyright 2009-2013, Barthélémy Dagenais.
- Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.1.
- </div>
- </div>
- </body>
- </html>
|