How to Start Appium Programmatically?

I have Tried following but getting syntax error. Please provide me solution
CommandLine command = new CommandLine();
command.addArgument(“cmd);
command.addArgument(”\c");
command.addArgument("“C:/Program Files (x86)/Appium/node.exe”");
command.addArgument("“C:/Program Files (x86)/Appium/node_modules/appium/bin/Appium.js”");
commadn.addArgument("–address");

You can refer my sample: https://github.com/hominhtuong/appium/tree/master/AppiumSample

  1. Create appiumServer:

    public AppiumDriverLocalService appiumService() {
       if (appiumService == null) {
          String appiumJSPath = "/usr/local/lib/node_modules/appium/build/lib/main.js";
          builder = new AppiumServiceBuilder()
                            .withAppiumJS(new File(appiumJSPath))
                            .withIPAddress("0.0.0.0")
                            .usingAnyFreePort()
                            .withArgument(GeneralServerFlag.SESSION_OVERRIDE)
                            .withArgument(GeneralServerFlag.LOG_LEVEL, "error");
          appiumService = builder.build();
          appiumService.start();
          System.out.println("[Appium] started on " + appiumService.getUrl());
       }
    
       return appiumService;
    

    }

  2. Use it:
    driver = new IOSDriver(AppiumServer.shared().appiumService().getUrl(),capabilities);

1 Like

it’s been asked a while ago in Need to start and stop appium server programatically

1 Like