APPIUM Server Start and Stop through Java Programming

 
package demo;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.openqa.selenium.*;
public class HannaTestScript 
{
  AndroidDriver driver;
  static String Appium_Node_Path="C:\\Appium\\node.exe";
  static String Appium_JS_Path="C:\\Program Files\\Appium\\node_modules\\appium\\bin\\appium.js";
  static AppiumDriverLocalService service;
  static String service_url;
    
  @BeforeTest(enabled = true)
  public void appiumStart() throws InterruptedException, MalformedURLException 
  {
   
    service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().
                usingPort(4723).usingDriverExecutable(new File(Appium_Node_Path)).
                withAppiumJS(new File(Appium_JS_Path)));
    service.start();
    Thread.sleep(25000);
    service_url = service.getUrl().toString();
    
    
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName", "ZX1B32FFXF");
    capabilities.setCapability("appPackage", "com.hannainst.hannalab");
    capabilities.setCapability("appActivity","com.hannainst.hannalab.ManagerActivity");
    
    
    driver= new AndroidDriver(new URL(service_url),capabilities);
    
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   }
   @Test
   public void Test_AppLaunch() throws Exception 
   {
     try{
      
      WebDriverWait wait = new WebDriverWait(driver, 300);
      wait.until(ExpectedConditions.elementToBeClickable(By.className("android.widget.RelativeLayout")));
      System.out.println("TC0 :-App Launched Successfully");
      }catch(Throwable e)
      {
       System.out.println("TC0:-Test Case Failed");
      }
  }
  @AfterTest
  public void appiumStop() throws IOException 
  {
    driver.quit();  
    service.stop();
  }
}