title: Starting Appium programmatically - Connection Refused
body: <p>I want to automate the testing of a mobile app using the android emulator.
  I am trying to start Appium programmatically and then pass its IP and Port into Webdriver, to be used. </p>
  
  <p>Before I pass it to the Webdriver, I print out the Appium Service's IP/Port it is running on, and it is 0.0.0.0:4723, but I am getting a Connection Refused after passing it to Webdriver.</p>
  
  <p>If I manually start Appium, it is starting on 0.0.0.0:4723, just fine.</p>
  
  <p>Please find my code and StackTrace below.</p>
  
  <pre><code>public class AppiumServerJava {
  
  public AppiumDriverLocalService service;
  public AppiumServiceBuilder builder;
  public DesiredCapabilities cap;
  public WebDriver driver = null;
  public DesiredCapabilities dc;
  
  File root = new File(System.getProperty("user.dir"));
  File app = new File(root, "/src/test/api/app-qa.apk");
  
  public WebDriver startServer() {
  
      // apk Capabilities
      dc = new DesiredCapabilities();
      dc.setCapability("BROWSER_NAME", "Android");
      dc.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
      dc.setCapability("deviceName", "nexus5");
      dc.setCapability("app", app.getAbsolutePath());
      dc.setCapability("appPackage", "com.aa.android.qa");
      dc.setCapability("appActivity", "com.aa.android.view.SplashActivity");
  
      // Appium Capabilities
      cap = new DesiredCapabilities();
      cap.setCapability("noReset", "false");
  
      // Build the Appium Service
      builder = new AppiumServiceBuilder();
      builder.usingDriverExecutable(new File("/home/ninad/.linuxbrew/bin/node"));
      builder.withAppiumJS(new File("/usr/local/bin/appium"));
      builder.withIPAddress("0.0.0.0");
      builder.usingPort(4723);
      builder.withCapabilities(cap);
      builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
      builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");
  
      // Start the server with the builder
      try {
          service = AppiumDriverLocalService.buildService(builder);
          service.start();
          } catch (NullPointerException e) {
          e.printStackTrace();
          }
  
      String appiumServiceUrl = service.getUrl().toString();
      System.out.println("Appium URL " + appiumServiceUrl);
  
      try {
          driver = new AndroidDriver&lt;&gt;(new URL(appiumServiceUrl), dc);
          driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      } catch (MalformedURLException e) {
          e.printStackTrace();
      }
      return driver;
  }
  
  
  public void stopServer() {
      service.stop();
  }
  
  public boolean checkIfServerIsRunnning(int port) {
  
      boolean isServerRunning = false;
      ServerSocket serverSocket;
      try {
          serverSocket = new ServerSocket(port);
          serverSocket.close();
      } catch (IOException e) {
          // If control comes here, then it means that the port is in use
          isServerRunning = true;
      } finally {
          serverSocket = null;
      }
      return isServerRunning;
  }
  }
  
  
  AppiumServerJava appiumServer = new AppiumServerJava();
  public static WebDriver driver = null;
  
  
  @Before("@setup")
  public void setUp() {
  
      int port = 4723;
      try {
          if (!appiumServer.checkIfServerIsRunnning(port)) {
              appiumServer.startServer();
              appiumServer.stopServer();
          } else {
              System.out.println("Appium Server already running on Port - " + port);
          }
      } catch (NullPointerException e) {
          e.printStackTrace();
      }
  
      driver = appiumServer.startServer();
  }
  </code></pre>
  
  <p>Stack Trace:</p>
  
  <pre><code>Java.lang.NullPointerException
      at io.appium.java_client.service.local.AppiumDriverLocalService.destroyProcess(AppiumDriverLocalService.java:175)
      at io.appium.java_client.service.local.AppiumDriverLocalService.start(AppiumDriverLocalService.java:138)
      at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:57)
      at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at cucumber.runtime.Utils$1.call(Utils.java:40)
      at cucumber.runtime.Timeout.timeout(Timeout.java:16)
      at cucumber.runtime.Utils.invoke(Utils.java:34)
      at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
      at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
      at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
      at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
      at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
      at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
      at cucumber.runtime.Runtime.run(Runtime.java:122)
      at cucumber.api.cli.Main.run(Main.java:36)Appium URL http://0.0.0.0:4723/wd/hub
  
      at cucumber.api.cli.Main.main(Main.java:18)
  Failure in before hook:StepDefinitions.setUp()
  Message: org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
  Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
  System info: host: 'ninad-Gazelle', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-42-generic', java.version: '1.8.0_151'
  Driver info: driver.version: AndroidDriver
      at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:101)
      at java.util.Optional.orElseGet(Optional.java:267)
      at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:100)
      at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
      at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
      at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
      at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
      at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
      at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
      at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:137)
      at io.appium.java_client.DefaultGenericMobileDriver.&lt;init&gt;(DefaultGenericMobileDriver.java:38)
      at io.appium.java_client.AppiumDriver.&lt;init&gt;(AppiumDriver.java:87)
      at io.appium.java_client.AppiumDriver.&lt;init&gt;(AppiumDriver.java:111)
      at io.appium.java_client.android.AndroidDriver.&lt;init&gt;(AndroidDriver.java:72)
      at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:66)
      at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at cucumber.runtime.Utils$1.call(Utils.java:40)
      at cucumber.runtime.Timeout.timeout(Timeout.java:16)
      at cucumber.runtime.Utils.invoke(Utils.java:34)
      at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
      at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
      at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
      at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
      at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
      at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
      at cucumber.runtime.Runtime.run(Runtime.java:122)
      at cucumber.api.cli.Main.run(Main.java:36)
      at cucumber.api.cli.Main.main(Main.java:18)
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
      at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
      at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
      at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
      at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
      at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
      at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:337)
      at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:136)
      at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
      at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
      ... 29 more
  
        org.openqa.selenium.WebDriverException: Connection refused (Connection refused)
        Build info: version: '3.5.2', revision: '10229a9', time: '2017-08-21T17:29:55.15Z'
        System info: host: 'ninad-Gazelle', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.10.0-42-generic', java.version: '1.8.0_151'
        Driver info: driver.version: AndroidDriver
          at io.appium.java_client.remote.AppiumCommandExecutor.lambda$2(AppiumCommandExecutor.java:101)
          at java.util.Optional.orElseGet(Optional.java:267)
          at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:100)
          at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
          at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
          at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
          at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
          at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:254)
          at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
          at org.openqa.selenium.remote.RemoteWebDriver.&lt;init&gt;(RemoteWebDriver.java:137)
          at io.appium.java_client.DefaultGenericMobileDriver.&lt;init&gt;(DefaultGenericMobileDriver.java:38)
          at io.appium.java_client.AppiumDriver.&lt;init&gt;(AppiumDriver.java:87)
          at io.appium.java_client.AppiumDriver.&lt;init&gt;(AppiumDriver.java:111)
          at io.appium.java_client.android.AndroidDriver.&lt;init&gt;(AndroidDriver.java:72)
          at Utilities.AppiumServerJava.startServer(AppiumServerJava.java:66)
          at mobileTest.appiumSeleniumCucumberAndroid.StepDefinitions.setUp(StepDefinitions.java:21)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
          at cucumber.runtime.Utils$1.call(Utils.java:40)
          at cucumber.runtime.Timeout.timeout(Timeout.java:16)
          at cucumber.runtime.Utils.invoke(Utils.java:34)
          at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)
          at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
          at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
          at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
          at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
          at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
          at cucumber.runtime.Runtime.run(Runtime.java:122)
          at cucumber.api.cli.Main.run(Main.java:36)
          at cucumber.api.cli.Main.main(Main.java:18)
        Caused by: java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204)
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
          at java.net.Socket.connect(Socket.java:589)
          at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75)
          at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
          at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
          at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
          at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
          at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
          at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
          at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
          at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
          at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
          at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
          at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:337)
          at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:136)
          at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
          at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
          ... 29 more
  </code></pre>