How to start Appium server 2.0 Programmatically

I am aware of starting Appium 1.x programmatically but not able to do it for Appium 2.0

AppiumServiceBuilder builder = new AppiumServiceBuilder();
    builder.usingAnyFreePort();
    builder.usingDriverExecutable(new File("<path to Node>\\node.exe"));
    builder.withAppiumJS(new File("<path to Appium>\\appium"));
    HashMap<String, String> environment = new HashMap();
    environment.put("PATH", "/usr/local/bin:" + System.getenv("PATH"));
    builder.withEnvironment(environment);
    service = AppiumDriverLocalService.buildService(builder);
    service.start();
    

0

install appium 2.0 using below command

npm install -g appium@next

Make sure there path for nodejs in system environment variable In User Variable add new Key NODE_PATH and APPIUM_PATH

For NODE_PATH put value as C:\Program Files\nodejs\node.exe

For APPIUM_PATH put value as C:\Users<user_name>\AppData\Roaming\npm\node_modules\appium\node_modules.bin

In this case it will start Appium Server 2.0 but test will still fail with No route found for /wd/hub/session"

I am working on it with reference to https://github.com/appium/appium/issues/15261