Java code to launch appium server through eclipse on OS X

I’m trying to launch the appium server from eclipse using JAVA on a MAC. I’ve read through the tutorials and documentation here, but none of them worked for me. I tried changing the port #s and the paths, but nothing worked. Perhaps, some one can help me troubleshoot?
Here’s the code I have so far:

public void serverStart() {
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(new CommandLine("/bin/sh/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --bootstrap-port 4723 --no-reset --show-ios-log --launch-timeout 120000"), resultHandler);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

This is the error I get:

org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: ‘2.42.1’, revision: ‘6a6995d’, time: ‘2014-06-21 03:12:34’
System info: host: ‘STammiraju’, ip: ‘10.143.8.39’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.10.5’, java.version: ‘1.7.0_79’
entry.getKey():“Driver info”

Where are you getting the Executor, DefaultExecutor, DefaultExecuteResultHandler, and CommandLine classes from? Executor isn’t the standard Java API defined Executor interface, since there is no setExitValue method defined in that particular interface. I’m currently assuming that this code spawns a child process that runs the command specified in the argument of the CommandLine constructor (why not use the ProcessBuilder class in the standard Java library?).

What is the code that sets up your Appium Driver (what are the lines involved that say new AndroidDriver(…) or new IOSDriver(…))?

Where are you getting the Executor, DefaultExecutor, DefaultExecuteResultHandler, and CommandLine classes from? I just borrowed ideas from other threads on this blog that were discussing issues similar to the one I’ve been facing. This is where I’ve been getting the help from:
Launching and stopping appium server programmtically
and
http://megettingerror.blogspot.in/2015/01/java-code-to-start-appium-server-on-mac.html
but it doesn’t seem to work of my code. Perhaps I’m missing something else?

What is the code that sets up your Appium Driver (what are the lines involved that say new AndroidDriver(…) or new IOSDriver(…))? I’m using appium’s DesiredCapabilities API that allows me to set up the iOS driver.

It’s possible that the Appium server wasn’t set up in a state ready for starting sessions by the time you called the Appium Driver constructor. Do you have some sort of wait code between spawning Appium and the constructor code?

My method to waiting was to poll “/wd/hub/status” at the address and port Appium is supposed to listen on until I received a valid response. At that point, I knew Appium was ready for starting a session; that’s when I would run the driver initialization code.

Tried this as well:

private static String APPIUMSERVERSTART = "/bin/sh/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --port 4723 --no-reset --local-timezone";
private static String NODESERVERSTART = "/usr/local/bin/node";

public static void startServer() throws IOException, InterruptedException{
Runtime runtime = Runtime.getRuntime();
process = Runtime.getRuntime().exec(APPIUMSERVERSTART);
Thread.sleep(5000);
if (process != null){
System.out.println(“appium server started”);
}
}

I have the sleep statement in there like you’ve said, but the server still doesn’t seem to launch.

Hmm… I’m not sure what’s up then. I’ll have to try this myself a little later and see what’s going on!

These are the different methods I’ve tried (independent of each other obviously) for launching the server. All of them show the same error message.

public void initializeServer() {
	
	 service = AppiumDriverLocalService
	 .buildService(new AppiumServiceBuilder()
	 .usingDriverExecutable(new File("/usr/local/bin/node"))
	 .withAppiumJS(
	 new File(
	 "/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js"))
	 .withIPAddress("127.0.0.1").usingPort(4723));	
	 service.start();
	
}

public static void startServer() throws IOException, InterruptedException{
	Runtime runtime = Runtime.getRuntime();
	Thread.sleep(100000);
	process = runtime.exec(NODESERVERSTART);
	process = runtime.exec(APPIUMSERVERSTART);
	process = Runtime.getRuntime().exec(APPIUMSERVERSTART);
	Thread.sleep(5000);
	if (process != null){
		System.out.println("appium server started");
	}
}



public void serverStart() {
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	Executor executor = new DefaultExecutor();
	executor.setExitValue(1);
	try {
		executor.execute(new CommandLine("/bin/sh/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js  --address 127.0.0.1 --bootstrap-port 4723 --no-reset --show-ios-log --launch-timeout 120000"), resultHandler);
	} catch (ExecuteException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

is node.js installed on u r MAC system

if u run this command in mac terminal
cd /usr/local/bin
./node
does this start node.js server

This is ideal way to start appium server, can u post error logs for this code
public void initializeServer() {
service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("/usr/local/bin/node"))
.withAppiumJS(
new File(
“/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js”))
.withIPAddress(“127.0.0.1”).usingPort(4723));
service.start();
if(service.isRunning())
System.out.println(“Appium server started successfully”);
}

I tried with this code it worked, using appium 1.3.7 dmg file and java client 3.2

public void initializeServer() {
service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("/Applications/Appium.app/Contents/Resources/mode/bin/node"))
.withAppiumJS(
new File(
“/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js”))
.withIPAddress(“127.0.0.1”).usingPort(4723));
service.start();
if(service.isRunning())
System.out.println(“Appium server started successfully”);
}

Thank you @amitjaincoer191 :smile: Yes, I have node.js installed. And I have the following versions:
Appium: 1.4.13
Xcode: 7.0.1
OS X: 10.10.5 (EL Capitan)
Eclipse: Mars Release (4.5.0)

This is the error I’m getting with the code you’ve shared:

[36minfoe[39m: [debug] And launch timeouts (in ms): {“global”:90000}
e[36minfoe[39m: [debug] e[33m[INST STDERR] dyld: could not load inserted library ‘/Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-instruments/thirdparty/iwd7/InstrumentsShim.dylib’ because no suitable image found. Did find:
/Applications/Appium.app/Contents/Resources/node_modules/appium/submodue[39m
e[36minfoe[39m: [debug] e[33m[INST STDERR] les/appium-instruments/thirdparty/iwd7/InstrumentsShim.dylib: mmap() error 1 at address=0x104BA4000, size=0x00001000 segment=__TEXT in Segment::map() mapping /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-instruments/thirdparty/iwd7/InstrumentsShim.dylib
e[39m
e[36minfoe[39m: [debug] e[90m[INSTSERVER] Instruments exited with code nulle[39m
e[36minfoe[39m: [debug] Killall instruments
[33mwarne[39m: Applications directory /Users/stammiraju/Library/Developer/CoreSimulator/Devices/442D41F0-0C60-4E0D-A3DF-EDC0F9EB06F4/data/Containers/Bundle/Application doesn’t exist. Have you run this simulator before?
e[36minfoe[39m: Couldn’t find app directories to delete. Probably it’s not installed
e[36minfoe[39m: [debug] Cleaning up appium session
e[31merrore[39m: Failed to start an Appium session, err was: Error: Instruments crashed on startup
e[36minfoe[39m: [debug] Error: Instruments crashed on startup
at [object Object].Instruments.onInstrumentsExit (/Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-instruments/lib/instruments.js:440:31)
at [object Object]. (/Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-instruments/lib/instruments.js:353:12)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
e[36minfoe[39m: [debug] Responding to client with error: {“status”:33,“value”:{“message”:“A new session could not be created. (Original error: Instruments crashed on startup)”,“origValue”:“Instruments crashed on startup”},“sessionId”:null}
e[36minfoe[39m: e[37m<-- POST /wd/hub/session e[39me[31m500e[39me[90m 20804.904 ms - 182e[39m e[90me[39m

Since you have those logs now, that means that the Appium server is being launched now. Did you figure out what was the cause for Appium not launching previously?

Did you figure out what was the cause for Appium not launching previously? Just a restart of my mac :wink: Appium server is being launched but the simulator doesn’t. “Instruments crashed” failure. Any idea as to how to over come that? :confused:

No clue about how to deal with Instruments. My opinion on Instruments is that it’s incredibly flaky, inconsistent, and unreliable. Instruments is one of the best implementations of Murphy’s Law in my experience.

If a restart is what is needed to get Instruments working, there’s really no way around it, short of Apple open sourcing Xcode (and Instruments) for the community to improve the tool.