Appium Server not able to stop by java programming

Hi Guys,

I was trying to start & stop appium server through java code. I’m able to start the server with the help of start() method but once stop() method execute console threw null pointer exception.
This is my code :

public class AppiumServerConnection
{
public static void main(String args[]) throws InterruptedException
{
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new
AppiumServiceBuilder()
.usingDriverExecutable(new File(“C:/Program
Files/nodejs/node.exe”)).withAppiumJS(new
File(“C:/Program Files (x86)/Appium/node_modules
/appium/bin/appium.js”))
.withIPAddress(“127.0.0.1”)
.usingPort(4726));

    service.start();
    System.out.println("Appium Server has been started!!");
    System.out.println("Current status of appium server : "+service.isRunning());
    Thread.sleep(5000);
    
    service.stop();
    System.out.println("Current status of appium server : "+service.isRunning());
    
    
}

}

Console Output:

e[36minfoe[39m: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)
e[36minfoe[39m: Appium REST http interface listener started on 127.0.0.1:4726
e[36minfoe[39m: [debug] Non-default server args: {“address”:“127.0.0.1”,“port”:4726}
e[36minfoe[39m: Console LogLevel: debug
e[36minfoe[39m: e[37m–>e[39m e[37mGETe[39m e[37m/wd/hub/statuse[39m e[90m{}e[39m
e[36minfoe[39m: [debug] Responding to client with success: {“status”:0,“value”:{“build”:{“version”:“1.4.16”,“revision”:“ae6877eff263066b26328d457bd285c0cc62430d”}}}
e[36minfoe[39m: e[37m<-- GET /wd/hub/status e[39me[32m200e[39me[90m 7.954 ms - 105e[39m e[90m{“status”:0,“value”:{“build”:{“version”:“1.4.16”,“revision”:“ae6877eff263066b26328d457bd285c0cc62430d”}}}e[39m
Appium Server has been started!!
e[36minfoe[39m: e[37m–>e[39m e[37mGETe[39m e[37m/wd/hub/statuse[39m e[90m{}e[39m
e[36minfoe[39m: [debug] Responding to client with success: {“status”:0,“value”:{“build”:{“version”:“1.4.16”,“revision”:“ae6877eff263066b26328d457bd285c0cc62430d”}}}
Current status of appium server : true
e[36minfoe[39m: e[37m<-- GET /wd/hub/status e[39me[32m200e[39me[90m 5.989 ms - 105e[39m e[90m{“status”:0,“value”:{“build”:{“version”:“1.4.16”,“revision”:“ae6877eff263066b26328d457bd285c0cc62430d”}}}e[39m
Aug 05, 2016 12:23:30 PM org.openqa.selenium.os.ProcessUtils killWinProcess
WARNING: Process refused to die after 10 seconds, and couldn’t taskkill it
java.lang.NullPointerException: Unable to find executable for: taskkill
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:251)
at org.openqa.selenium.os.UnixProcess.(UnixProcess.java:62)
at org.openqa.selenium.os.CommandLine.(CommandLine.java:38)
at org.openqa.selenium.os.WindowsUtils.killPID(WindowsUtils.java:178)
at org.openqa.selenium.os.ProcessUtils.killWinProcess(ProcessUtils.java:138)
at org.openqa.selenium.os.ProcessUtils.killProcess(ProcessUtils.java:81)
at org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.destroyHarder(UnixProcess.java:247)
at org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.access$2(UnixProcess.java:246)
at org.openqa.selenium.os.UnixProcess.destroy(UnixProcess.java:125)
at org.openqa.selenium.os.CommandLine.destroy(CommandLine.java:155)
at io.appium.java_client.service.local.AppiumDriverLocalService.destroyProcess(AppiumDriverLocalService.java:178)
at io.appium.java_client.service.local.AppiumDriverLocalService.stop(AppiumDriverLocalService.java:168)
at pe.android.appiumserver.AppiumServerConnection.main(AppiumServerConnection.java:28)

Exception in thread “main” java.lang.RuntimeException: Process refused to die after 10 seconds, and couldn’t taskkill it: Unable to find executable for: taskkill
at org.openqa.selenium.os.ProcessUtils.killWinProcess(ProcessUtils.java:142)
at org.openqa.selenium.os.ProcessUtils.killProcess(ProcessUtils.java:81)
at org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.destroyHarder(UnixProcess.java:247)
at org.openqa.selenium.os.UnixProcess$SeleniumWatchDog.access$2(UnixProcess.java:246)
at org.openqa.selenium.os.UnixProcess.destroy(UnixProcess.java:125)
at org.openqa.selenium.os.CommandLine.destroy(CommandLine.java:155)
at io.appium.java_client.service.local.AppiumDriverLocalService.destroyProcess(AppiumDriverLocalService.java:178)
at io.appium.java_client.service.local.AppiumDriverLocalService.stop(AppiumDriverLocalService.java:168)
at pe.android.appiumserver.AppiumServerConnection.main(AppiumServerConnection.java:28)
Caused by: java.lang.NullPointerException: Unable to find executable for: taskkill
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:251)
at org.openqa.selenium.os.UnixProcess.(UnixProcess.java:62)
at org.openqa.selenium.os.CommandLine.(CommandLine.java:38)
at org.openqa.selenium.os.WindowsUtils.killPID(WindowsUtils.java:178)
at org.openqa.selenium.os.ProcessUtils.killWinProcess(ProcessUtils.java:138)
… 8 more

Please guys help me with this issue.
Thanks in advance.

Here is the working code for launching the appium and closing the server pragmatically:

public static void appiumStart() throws IOException, InterruptedException {
String cmd = {“C:\WINDOWS\system32\cmd.exe”,“/c”, “start”, nodePath, appiumJSPath};
p = Runtime.getRuntime().exec(cmd);
// If face any error(Could not start a new session…) then Increase this time to 15 or 20 or 25 seconds.
Thread.sleep(25000);
if (p != null)
{
System.out.println(“Appium server Is started now.”);
}
}

// This method Is responsible for stopping appium server.
public static void appiumStop() throws IOException
{
if (p != null)
{
p.destroy();
}
System.out.println(“Appium server Is stopped now.”);
}

@BeforeSuite
public static void setUP() {
appiumStop();
appiumStart();
}

@AfterSuite
public static void tearDown()
{
CommonUtils.report.flush();
driver.closeApp();
driver.quit();

  //to close the command prompt
  try {
  	Runtime.getRuntime().exec("taskkill /f /im node.exe") ;
  	System.out.println("Closed the node.exe");
  } catch (Exception e) {
  	e.printStackTrace();
  }

}