title: Simplest example with py4J
body: <p>I installed <code>py4J</code> using pip on my <code>conda</code> virtual environment in Python.  I wrote a super simple example <code>AdditionApplication.java</code> to test <code>py4J</code>, but it fails to compile, i.e.</p>
  
  <p><code>javac AdditionApplication.java</code> </p>
  
  <p>fails complaining that  <code>GatewayServer</code> is not defined. </p>
  
  <p>I am knowledgeable in Python but unfortunately not in Java. What else do I need to provide?</p>
  
  <pre><code>public class AdditionApplication {
  
    public int addition(int first, int second) {
      return first + second;
    }
  
    public static void main(String[] args) {
      AdditionApplication app = new AdditionApplication();
      // app is now the gateway.entry_point
      GatewayServer server = new GatewayServer(app);
      server.start();
    }
  }
  </code></pre>
  
  <p>In case it matters I have the following version of Java installed:</p>
  
  <pre><code>java -version
  java version "1.7.0_51"
  Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
  Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
  </code></pre>
  
  <h2>Update 1</h2>
  
  <p>After I added: <code>import py4j.GatewayServer;</code> to the top of the file, I got a different error:</p>
  
  <p><code>package py4j does not exist</code></p>
  
  <h2>Update 2</h2>
  
  <p><code>pip install py4j</code> left a <code>jar</code> file under <code>&lt;PATH_TO_CONDA_ENVIRONMENT&gt;/share/py4j/py4j0.8.1.jar</code>. I have added it to my class path with:</p>
  
  <pre><code>javac -cp &lt;PATH_TO_CONDA_ENVIRONMENT&gt;/share/py4j/py4j0.8.1.jar AdditionApplication.java
  </code></pre>
  
  <p>and it output</p>
  
  <p><code>AdditionApplication.class</code></p>
  
  <p>How do I run it?</p>
  
  <h1>Final update and solution:</h1>
  
  <p>After applying the previous fixes, I finally run the code with:</p>
  
  <pre><code>java -cp &lt;PATH_TO_CONDA_ENVIRONMENT&gt;/share/py4j/py4j0.8.1.jar AdditionApplication 
  </code></pre>
  
  <p>the code runs in the background. To test it:</p>
  
  <pre><code>&gt;&gt;&gt; from py4j.java_gateway import JavaGateway
  &gt;&gt;&gt; gateway = JavaGateway()                   # connect to the JVM
  &gt;&gt;&gt; random = gateway.jvm.java.util.Random()   # create a java.util.Random instance
  &gt;&gt;&gt; number1 = random.nextInt(10)              # call the Random.nextInt method
  &gt;&gt;&gt; number2 = random.nextInt(10)
  &gt;&gt;&gt; print(number1,number2)
  (2, 7)
  &gt;&gt;&gt; addition_app = gateway.entry_point        # get the AdditionApplication instance
  &gt;&gt;&gt; addition_app.addition(number1,number2)    # call the addition method
  </code></pre>