Start appium server programmatically with iPhone is failing

When I start the appium server via terminal tests run successfully.
So now I tried to run the appium programmatically with the below code (this code works fine on Android).

Once I execute the code it fails on iPhone with the below error (see log below).
The error state “Install with ‘brew install libimobiledevice --HEAD’” however libimobiledevice is already installed.

For those who run iPhone programmatically can you assist?

ENV:
Appium - 1.6.4 beta
Platform- iPhone
Client libraries - Java-Client 5.0.0 beta5

==============================================
public AppiumDriverLocalService startAppiumService() {

	 AppiumDriverLocalService service =
	 AppiumDriverLocalService.buildDefaultService();

	boolean isServiceRunning =  service.isRunning();
	if (isServiceRunning){
		
		service.stop();
	}
	service.start();
	return service;

}

=========================================

Error log:

TestNG] Running:
/Users/menypeled/Documents/workspace/iOS_Appium/src/resources/Testng.xml

Please choose Environment mode(1 for QA, 2 for Staging or 3 for PROD):1
Testing against QA Environment
Do you want to use Applitools eye?(1 for Yes, or continue for No):2
Testing without Applitools visual testing
[Appium] Welcome to Appium v1.6.4-beta
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> GET /wd/hub/status {}
[debug] [MJSONWP] Calling AppiumDriver.getStatus() with args: []
[debug] [MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.6.4-beta”,“revision”:null}}
[HTTP] <-- GET /wd/hub/status 200 15 ms - 88
[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“app”:"/Users/menypeled/Documents/workspace/iOS_Appium/SkyGiraffe.app",“newCommandTimeout”:100000,“platformVersion”:“10.2.1”,“xcodeOrgId”:“7Y5J2RJXYV”,“automationName”:“XCUITest”,“udid”:“51d8c712258a5b0ac63516aa5be9d10014b0c270”,“platformName”:“iOS”,“deviceName”:“SG iPhone 5s”,“updatedWDABundleId”:“beta.SkyGiraffe.SkyGiraffe”,“xcodeSigningId”:“iPhone Developer”}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{“app”:"/Users/menypeled/Documents/workspace/iOS_Appium/SkyGiraffe.app",“newCommandTimeout”:100000,“platformVersion”:“10.2.1”,“xcodeOrgId”:“7Y5J2RJXYV”,“automationName”:“XCUITest”,“udid”:“51d8c712258a5b0ac63516aa5be9d10014b0c270”,“platformName”:“iOS”,“deviceName”:“SG iPhone 5s”,“updatedWDABundleId”:“beta.SkyGiraffe.SkyGiraffe”,“xcodeSigningId”:“iPhone Developer”},null,null,null,null]
[debug] [BaseDriver] Event ‘newSessionRequested’ logged at 1489417944676 (17:12:24 GMT+0200 (IST))
[Appium] Creating new XCUITestDriver (v2.19.0) session
[Appium] Capabilities:
[Appium] app: ‘/Users/menypeled/Documents/workspace/iOS_Appium/SkyGiraffe.app’
[Appium] newCommandTimeout: 100000
[Appium] platformVersion: ‘10.2.1’
[Appium] xcodeOrgId: ‘7Y5J2RJXYV’
[Appium] automationName: ‘XCUITest’
[Appium] udid: ‘51d8c712258a5b0ac63516aa5be9d10014b0c270’
[Appium] platformName: ‘iOS’
[Appium] deviceName: ‘SG iPhone 5s’
[Appium] updatedWDABundleId: ‘beta.SkyGiraffe.SkyGiraffe’
[Appium] xcodeSigningId: ‘iPhone Developer’
[debug] [XCUITest] XCUITestDriver version: 2.19.0
[BaseDriver] Session created with session id: dc281c14-27d3-4b22-9ce6-8cb15f4f09ff
[debug] [XCUITest] Xcode version set to ‘8.2.1’ (tools v8.2.0.0.1.1480973914)
[debug] [XCUITest] iOS SDK Version set to ‘10.2’
[debug] [BaseDriver] Event ‘xcodeDetailsRetrieved’ logged at 1489417944804 (17:12:24 GMT+0200 (IST))
[XCUITest] The ‘idevice_id’ program is not installed. If you are running a real device test it is necessary. Install with ‘brew install libimobiledevice --HEAD’
[debug] [XCUITest] Available devices:
[XCUITest] Error: Unknown device or simulator UDID: ‘51d8c712258a5b0ac63516aa5be9d10014b0c270’
at XCUITestDriver.determineDevice$ (…/…/lib/driver.js:571:17)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at process._tickCallback (internal/process/next_tick.js:103:7)
Error: Unknown device or simulator UDID: ‘51d8c712258a5b0ac63516aa5be9d10014b0c270’
at XCUITestDriver.determineDevice$ (…/…/lib/driver.js:571:17)
at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at process._tickCallback (internal/process/next_tick.js:103:7)

You seem to have only a partial install of libimobiledevice. Try uninstalling it and then install it again as the instructions state. After that check to make sure idevice_id did get installed.

Tried to uninstall & install the libimobiledevice but the error remains

You may want to look at the install logs and see if there is an error installing idevice_id. You could also try installing it by itself.

The issue here is that $PATH is empty when you try launching the Appium server programatically. Therefore it is not able to find the program idevice_id.

When launching the command programmatically, also export the path before it. Run the following command:

‘export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin; YOUR_CURRENT_COMMAND_TO_LAUNCH_APPIUM_SERVER HERE’

Thanks for pointing out, it’s environmental issue only, so creating builder with environment.

This is the code, which solves the issue for me

		Map<String, String> env = new HashMap<>(System.getenv());
		env.put("PATH", "/usr/local/bin:" + env.get("PATH"));
                    AppiumServiceBuilder builder = new AppiumServiceBuilder()
				.withIPAddress("0.0.0.0")
				.usingPort(4723)
				.withEnvironment(env)
				.usingDriverExecutable(new File("/usr/local/bin/node"))
				.withAppiumJS(new File("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js"));
		
		service = AppiumDriverLocalService.buildService(builder);
                    service.start();

Thanks