title: py4j.protocol.Py4JNetworkError : An error occurred while trying to connect to the Java server
body: <p>I have the following simple example from the py4j document:</p>
  
  <pre><code>from py4j.java_gateway import JavaGateway
  
  def main():
  
      print("Hello")
      gateway = JavaGateway()                   # connect to the JVM
      random = gateway.jvm.java.util.Random()   # create a java.util.Random instance
      number1 = random.nextInt(10)              # call the Random.nextInt method
      number2 = random.nextInt(10)
      print(number1,number2)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>I tried to run it, but get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/edamameQ/github/myTest/main.py", line 16, in &lt;module&gt;
      main()
    File "/Users/edamameQ/github/myTest/main.py", line 7, in main
      random = gateway.jvm.java.util.Random()   # create a java.util.Random instance
    File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 1188, in __getattr__
      "\n" + proto.END_COMMAND_PART)
    File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 624, in send_command
      connection = self._get_connection()
    File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 579, in _get_connection
      connection = self._create_connection()
    File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 585, in _create_connection
      connection.start()
    File "/Library/Python/2.7/site-packages/py4j/java_gateway.py", line 697, in start
      raise Py4JNetworkError(msg, e)
  py4j.protocol.Py4JNetworkError: An error occurred while trying to connect to the Java server
  </code></pre>
  
  <hr>
  
  <p>Is there anything I should do in order to connect to a Java server? Thanks!</p>