Start and Stop Appium Server programmatically

There are multiple ways to start appium service, manually we can start by clicking on appium icon, start using command prompt and programmatically. In this tutorial, let us see how to start Appium server programmatically. We can achieve this with the help of Appium java Client 'AppiumDriverLocalService' class.

We will see different ways of doing this. We need to start appium and stop the appium service once we are done with tests as 'appiumservice.start()' and 'appiumservice.stop()'. With the help of 'buildDefaultService()' we can easily start and stop appium service.

Let us look at the simple example to start and stop appium server.

package com.appium.adnroid;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class AndroidCalcAppExample {
	WebDriver driver;
    AppiumDriverLocalService appiumService;
    String appiumServiceUrl;
	public void setUp() throws MalformedURLException {
        appiumService = AppiumDriverLocalService.buildDefaultService();
        appiumServiceUrl = appiumService.getUrl().toString();
        System.out.println("Appium Service Address : - "+ appiumServiceUrl);
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("deviceName", "8d75c47d");
		capabilities.setCapability("platformName", "Android");
		capabilities.setCapability("appPackage", "");
		capabilities.setCapability("appActivity", "");
		driver = new AndroidDriver<>(new URL(appiumServiceUrl), capabilities);
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

	public void Sum() {
		System.out.println("Calculate sum of two numbers");
		// Locate elements to enter data and click +/= buttons

		// Get the result text
		WebElement sumOfNumbersEle = driver.findElement(By.className("android.widget.EditText"));
		String sumOfNumbers = sumOfNumbersEle.getText();

		// verify if result is 3

	public void End() {
		System.out.println("Stop driver");
		System.out.println("Stop appium service");


There is also an other way to start appium service as below by using 'AppiumDriverLocalService' class and 'AppiumServiceBuilder'

But we need to provide path of Appium node.exe file path and appium.js file path as below :-

String Appium_Node_Path="C:/Program Files/Appium/node.exe";
String Appium_JS_Path="C:/Program Files/Appium/node_modules/appium/bin/appium.js";
AppiumDriverLocalService appiumService;

appiumService = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
	                usingAnyFreePort().usingDriverExecutable(new File(Appium_Node_Path)).
	                withAppiumJS(new File(Appium_JS_Path)));

In the above code, we are using method 'usingAnyFreePort()' which Configures the appium server to start on any available port. If we want to execute tests on any specific port, we have to use method 'usingPort(port value)' which sets the port on which appium server has to be started.

Appium Tutorials: 

Add new comment