package org.obolibrary.robot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import py4j.GatewayServer;

/**
 * Starts a gateway server for Py4J to execute ROBOT operations via Python. This class can be used
 * to start the JVM directly from python using Py4J's `launch_gateway`.
 *
 * @author <a href="mailto:rbca.jackson@gmail.com">Becky Jackson</a>
 */
public class PythonOperation {

  /** Logger. */
  private static final Logger logger = LoggerFactory.getLogger(PythonOperation.class);

  /**
   * Run a Gateway Server.
   *
   * @param args strings to use as arguments
   */
  public void main(String[] args) {
    run(null);
  }

  /**
   * Run a Gateway Server.
   *
   * @param port port to run JVM on, or null
   */
  public static void run(Integer port) {
    GatewayServer gs;
    if (port != null) {
      gs = new GatewayServer(null, port);
    } else {
      gs = new GatewayServer(null);
    }
    try {
      gs.start();
      port = gs.getPort();
      logger.debug(String.format("JVM started on port %d", port));
    } finally {
      gs.shutdown();
    }
  }
}

@@ -630,18 +630,141 @@
     
-run(null);
+GatewayServer gs = null;%0A    try %7B%0A      gs = run(null);%0A    %7D finally %7B%0A      if (gs != null) %7B%0A        gs.shutdown();%0A      %7D%0A    %7D
 %0A  %7D
@@ -870,20 +870,29 @@
  static 
-void
+GatewayServer
  run(Int
@@ -1047,28 +1047,16 @@
 ;%0A    %7D%0A
-    try %7B%0A  
     gs.s
@@ -1059,26 +1059,24 @@
 gs.start();%0A
-  
     port = g
@@ -1084,26 +1084,24 @@
 .getPort();%0A
-  
     logger.d
@@ -1161,46 +1161,18 @@
     
-%7D finally %7B%0A      gs.shutdown();%0A    %7D
+return gs;
 %0A  %7D

Make sure JVM doesn't shutdown from command line