Sunday, 31 January 2016

How to Start Appium Server using Java Code?

 We have seen how to start and stop Appium server manually and now in this post we will come to know how to start and stop Appium server programmatically. Appium provide APIs so that you can start the server before running your test case or test suit and then you can stop the server once the execution is over.

Once you install appium you the files node.exe and appium.js will be there is your system. You need the path of these two files for starting apium server programmatically. Then you can copy and paste the below code. Call appiumStart() in @BeforeClass or @BeforeTest .

Required Jar files are commons-validator-1.4.1.jar and java-client-3.2.0.jar which you can download from here.

Video -

Explanation about the code -

AppiumDriverLocalService class provied the api to start and stop the server hence we have used this in our code below.usingPort() method is used to provide port number for starting the server. We ned to pass our node.exe path to method usingDriverLocation() and appium.js path to withAppiumJs() method. Then start() and stop() methods are used to start and stop the server. We need to use getUrl() method to get and pass the url while setting up the Desired Capabilities.


import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class AppiumServerStartStop {

    static String Appium_Node_Path="C:\\Program Files (x86)\\Appium\\node.exe";
    static String Appium_JS_Path="C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js";
    static AppiumDriverLocalService service;
    static String service_url;

    public static void appiumStart() throws Exception{
        service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
                usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).
                withAppiumJS(new File(Appium_JS_Path)));
        service_url = service.getUrl().toString();

    public static void appiumStop() throws Exception{


Note use the service_url like shown below while setting up Desired Capabilities-

AppiumDriver driver= new AndroidDriver(new URL(service_url),cap);

If you find this Post useful do share with your friends and if you have some questions or suggestions do share them with me in the comments section below.Please follow QA Automated for latest updates.



  1. Hi Anuja. Thanks for your example. But i am facing an issue using your code and running the test using maven command.
    "Couldn't start Appium REST http interface listener. Requested port is already in use. PLease make sure there's no other instance of appium running already."

    Please help me if you know the fix. Thanks in advance

    1. This means your appium server instance is runnig with same port. You can try with changing port number. make the change in below line of code.
      service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
      usingPort(2856).usingDriverExecutable(new File(Appium_Node_Path)).

  2. I have started Appium through NODE...the below is from jenkins..

    /usr/local/bin/node /Applications/ --address --port 4723/wd/hub

    how do i start and make the tests of now it just starts appium and does nothing :(

    Started by user anonymous
    [EnvInject] - Loading node environment variables.
    Building in workspace /Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/​
    [​] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/
    + cd /Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/
    + chmod g+w+x
    + sh
    [Appium] Welcome to Appium v1.5.3
    [Appium] Non-default server args:
    [Appium] app: '/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/'
    [Appium] Deprecated server args:
    [Appium] --app => --default-capabilities '{"app":"/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/"}'
    [Appium] Default capabilities, which will be added to each request unless overridden by desired capabilities:
    [Appium] app: '/Users/Shared/Jenkins/Home/jobs/MMLocalProject/workspace/src/T/result/'
    [Appium] Appium REST http interface listener started on

  3. Works like charm after resolving dependencies on few libraries. Thanks a lot.

  4. i have installed appium 1.6 using npm but programmatically it still opens up appium 1.4 could i resolve

  5. can i use this appium for web based application and how?

    1. Hi , you can use appium to test mobile application having webviews plus you can test web applications in mobile browser check out my appium tutorial for How to Test Web Views using Appium (

  6. Hi Anuja,
    Could please you make the same tutorial for MacOS ?
    Thank you in advance.

  7. There is a slight changes happened as per new appium server Appium v1.7.0

    ** No need to specify the Android js path only Node js is enough.

    code is as below.

    public void appiumStart() throws Exception
    service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
    .usingDriverExecutable(new File(Appium_Node_Path))
    System.out.println("---- Service Started-----");
    service_url = service.getUrl().toString();

    driver= new AndroidDriver(new URL(service_url),capabilities);

  8. Yes this site is very useful.....i was struggling from long time to install appium before i browsed into this site.

  9. import { ipcMain, BrowserWindow, Menu } from 'electron';
    SyntaxError: Unexpected token import
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
    at startup (bootstrap_node.js:150:9)
    this is the error i am getting while compiling the code...can you help me resolve it

  10. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Java Online Training from India . or learn thru Online Training mode Hibernate Online Training . Nowadays Hibernate ORM has tons of job opportunities on various vertical industry. or learn thru JavaScript Online Training from India. Nowadays even JavaScript has tons of job opportunities on various vertical industry.