This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
21 / 205
Nov 2014

[Update] For a complete updated guide with code example, please refer to this1.1k.

@karthik_holla
I am gonna use Java and Windows to help explain that to you, you can use the equivalent of that in your preferred language/OS:

  • To start Appium server programmatically, you need the following:

    1. Command executor tool (I prefer the second approach as it will handle most of the OS specific problems on your behalf):
    2. Tell the tool what program you need to start. This is OS dependent and differs from OS version to another in some cases. You can use the next table to find the one matching your environment.
      • Windows: cmd /c
      • Linux: /bin/env
      • Mac: /bin/sh
    3. Invoke the NodeJS server and pass the arguments it needs to start the server. You can do that using the following command (Replace C:/Program Files (x86) with the path to your Appium installation folder):

      "C:/Program Files (x86)/Appium/node.exe" "C:/Program Files (x86)/Appium/node_modules/appium/bin/Appium.js"

    4. The last step is to provide the Appium server arguments as normal.

Putting all that together, you formulate a command like:

cmd /c "C:/Program Files (x86)/Appium/node.exe" "C:/Program Files (x86)/Appium/node_modules/appium/bin/Appium.js" --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --selendroid-port 8082 --no-reset --local-timezone

To execute that command using Apache Commons Exec:

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(new CommandLine("The command you just formulated above"), resultHandler);

I am not sure if it's the best approach, but it works just fine with me. @bootstraponline @jlipps Can you please confirm if this is the best approach?


  • To stop Appium server, just replace step 3 above by:

    • Windows: taskkill /F /IM node.exe
    • Linux: killall node
    • Mac: killall node

Note: In case of starting more than one server instance on the same machine, you have to replace the killall command to prevent killing the server of the first instance after staring a second one. Sadly I hit a dead-end in this part (I am currently leaving the server instances opened and I close them manually after the test is done until I find a better approach to killing the server in a more smart way).


  • To capture all Appium log messages in a file, add the following flag to your Appium server arguments:

    --log "log-file-path"

Hope this helps. Just ask if anything is not clear :smile:

@Hassan_Radi
Thank for such an elaborate explanation. However i am new to Appium so have some tough time trying to understand what needs to be done.

Can you please suggest what needs to be passed as parameters to executor.execute(new CommandLine("The command you just formulated above"), resultHandler);

Also can you please explain the command which is to be executed?

cmd /c -->to start the command prompt
"C:/Program Files (x86)/Appium/node.exe"-->to start the node exe
"C:/Program Files (x86)/Appium/node_modules/appium/bin/Appium.js" --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --selendroid-port 8082 --no-reset --local-timezone-->more explanation on this would be really helpful.

i am using java and executing the scripts on windows 7.

thanks in advance,
Karthik

@karthik_holla
This is responsible for starting the Appium server:

This represents the Appium server arguments196. You use whatever you want to start the server with (I was just giving you an example of how to do it).

To create the CommandLine object from the command, do that:

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");

Keep passing all the parameters as above. Keep in mind that you add a new argument for each space separated part of the command.

@Hassan_Radi,

thank you for explaining things. I was able to launch appium server. It even got connected to the device. But, failed while launching the appium driver with the error "Cannot bind to socket". Below is the code snippet which launches the appium server.

System.out.println("driver being created...");
CommandLine command = new CommandLine("cmd");
//command.addArgument("cmd");
command.addArgument("/c");
command.addArgument("C:\Users\mkarthik\Documents\selenium\Appium\node.exe");
command.addArgument("C:\Users\mkarthik\Documents\selenium\Appium\node_modules\appium\bin\appium.js\");
command.addArgument("--address");
command.addArgument("127.0.0.1");
command.addArgument("--bootstrap-port");
command.addArgument("4242");
command.addArgument("--no-reset");
command.addArgument("--log");
command.addArgument("C:\Users\mkarthik\workspace\Android\AppiumMobileAutomation\src\com\log\appiumLogs.txt");
resultHandler = new DefaultExecuteResultHandler();
executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);

Please can you advice what needs to overcome this failure. Also i am unable to see anything getting stored in logs.
i have attached the appium server logs.

appium server logs.txt106 (7.1 KB)

Hi @Hassan_Radi,

i was able to execute the script by launching the server programmatically.

Just a question: when i changed command.addArgument("4244"); to command.addArgument("4724"); it worked.

sbootstrap-port would always refer to the device port where appium server forwards the command?

please help as it would make things much clearer to understand.

Thanks and regards,
M.Karthik

As the Appium documentation55 mentions,

--bootstrap-port : port to use on device to talk to Appium server

I never change the bootstrap-port, changing it and acting in this way might be a bug in Appium. @bootstraponline @jlipps Is that a bug??

As of the other problem you mentioned, that nothing is stored in the logs: I took a look at the log you provided and I can see that Appium logs are present and I quote:

[36minfo[39m: Welcome to Appium v1.2.0 (REV e53f49c706a25242e66d36685c268b599cc18da5)
[90mdebug[39m: Non-default server args: {"address":"127.0.0.1","bootstrapPort":4242,"noReset":true,"log":"C:\Users\mkarthik\workspace\Android\AppiumMobileAutomation\src\com\log\appiumLogs.txt"}
[36minfo[39m: Appium REST http interface listener started on 127.0.0.1:4723
[36minfo[39m: LogLevel: debug

1 month later

Thanks for providing this useful info, helped me a lot.

This is what worked for me..
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("C://Users//....//Appium//node.exe");
command.addArgument("C://Users//...//Appium//node_modules//appium//bin//appium.js");
command.addArgument("--address");
command.addArgument("0.0.0.0");
command.addArgument("--port");
command.addArgument("4724");
command.addArgument("--no-reset");
command.addArgument("--log");
command.addArgument("C://Users//...//log//appiumLogs.txt");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);

As I mentioned in the main post:

You will end up with something like this:

CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("taskkill");
command.addArgument("/F");
command.addArgument("/IM");
command.addArgument("node.exe");

This does not work for me.

When i try to re-run the test, I see the error:
[31merror[39m: Couldn't start Appium REST http interface listener. Requested port is already in use. Please make sure there's no other instance of Appium running already.

I have to manually kill the process from Windows Task Manager and the re-run the test.

It works just fine with me .... Are you sure you are not doing something wrong??

Hey, Guys

I use processBuilder for that. It has method destroy().
You could check my example in the thread:

10 days later

I thought about using it at first, but it turned out that it doesn't destroy any spawned processes started by your application. You will end up with memory leaks and probably some errors when you try to open it again.

It perfectly closes child processes. As you can see, there is a process tree and when cmd.exe (with PID 6320) get closed - all child processes get closed as well

As I understood from the image you shared, you are starting a new JVM inside the original one. That's why when you call the destroy function, the Java process and all of its child processes are destroyed. In my case, I was starting the Appium server from the original JVM which means that all of its spawned processes won't be destroyed until the original Java JVM terminates.

P.S: I am running more than one server instance simultaneously and I can't wait to clear up memory at the end when the JVM terminates.

Parent java process from the screenshot above - is IDE:-)
Just test run via "mvn test" command-line and got the same result: all processes closed perfectly!

Looks like it's working :smile: with the destry method and there is no need to taskill the process .... I am not sure why it wasn't working with me when I tried it?
Anyway, I will give it another try. Thanks :smile:

Hi Everyone,

When I tried to launch the server, I am geeting this below error:
" /Applications/Appium.app/Contents/Resources/node/bin/node: cannot execute binary file "

My code is this, Can anyone please help on this ?
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
command.addArgument("--address");
command.addArgument("127.0.0.1");
command.addArgument("--port");
command.addArgument("4723");
command.addArgument("--no-reset");
command.addArgument("--log");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);

Thanks in advance.

@Hassan_Radi : Still getting the same error.
I am using Appium version : 1.3.1 (Ophiuchus)

below is my entire code-
public static void main(String[] args) throws ExecuteException, IOException {
CommandLine command = new CommandLine("/bin/sh");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
command.addArgument("--address");
command.addArgument("127.0.0.1");
command.addArgument("--port");
command.addArgument("4723");
command.addArgument("--no-reset");
command.addArgument("--log");
command.addArgument("argument", false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
}

I saw this line in your post,
"command.addArgument("/c");"
what does it do exactly ?

By that, I didn't mean adding it explicity :smile: ... I meant to convert each line that uses addArgument function and make it take an argument and a false value :smile:.

for example:

command.addArgument("--address");

should be

command.addArgument("--address", false);

@Hassan_Radi: LOL !!

But still getting the same error, do you think its because of any device incompatibility ?
Because all I get in searching internet about this is 32-bit vs 64-bit issues only.

OS X : Version 10.8.5
Appium version : 1.3.1 (Ophiuchus)

I am running below code pn my mac no error but appium is not started as well.
Where as if i involve from terminal this command : /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --port 4723 --no-reset --local-timezone

code::

CommandLine command = new CommandLine("/bin/sh -c");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node",false);
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js",false);
command.addArgument("--address",false);
command.addArgument("127.0.0.1");
command.addArgument("--port",false);
command.addArgument("4723");
command.addArgument("--no-reset",false);
command.addArgument("--log");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I guess I missed "-c" while invoking CommandLine. Its not showing the error, but like @Sachin_Chauhan on running the code nothing is happening.

on windows with windows format command its working. But on mac its doing nothing not even error. Now I am stuck. I appreciate if someone guide me, what I am doing wrong?

As soon as I removed -c, this exception started coming
/Applications/Appium.app/Contents/Resources/node/bin/node: /Applications/Appium.app/Contents/Resources/node/bin/node: cannot execute binary file

I left the hope with command line. Now I am using process builder and its working ok.

List list = new ArrayList();
list.add("/Applications/Appium.app/Contents/Resources/node/bin/node");
list.add("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js");
list.add("--address");
list.add("127.0.0.1");
list.add("--port");
list.add("4723");
list.add("--no-reset");
// create the process builder
ProcessBuilder pb = new ProcessBuilder(list);
try {
pb.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

I just tried with CommandLine and it worked after I changed:

CommandLine command = new CommandLine("/bin/sh -c");
command.addArgument("/Applications/Appium.app/Contents/Resources/node/bin/node",false);

to be:

CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");

I am not sure what has changed since the last time, but it's working now without invoking the shell app.

Give it a try and let me know how it works.

@BeforeMethod
public void setUp() throws Exception {
	
	Runtime.getRuntime().exec("/bin/bash export ANDROID_HOME=$HOME/Downloads/adt-bundle-mac-x86_64/sdk/");
	
	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--no-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	executor.execute(command, resultHandler);
	
	DesiredCapabilities capabilities = new DesiredCapabilities();
	capabilities.setCapability("appium-version", "1.0");
	capabilities.setCapability("platformName", "iOS");
	capabilities.setCapability("platformVersion", "7.1");
	capabilities.setCapability("deviceName", "iPhone 6 Plus");
	capabilities.setCapability("app", "/Users/Prithivi/Documents/Sample.app");
	
	driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
	driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
	driver.findElement(By.name("Continue")).click();

This works for me very well... :smile:

Note: I used java-client-1.3.0.jar, in latest version AppiumDriver was changed into interface.

thanks @Prithivi and @Hassan_Radi mine also worked. But now I am not able to create session. Even I used your
Runtime.getRuntime().exec("/bin/bash export ANDROID_HOME=$HOME/Downloads/adt-bundle-mac-x86_64/sdk/");
to set the path then also, its showing me error as 31merror[39m: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
[36minfo[39m: [debug] Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at null. (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-adb/lib/adb.js:113:12)
at ChildProcess.exithandler (child_process.js:652:7)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket. (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)
[36minfo[39m: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.)","origValue":"Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path."},"sessionId":null}
[36minfo[39m: [37m<-- POST /wd/hub/session [39m[31m500[39m[90m 7040.310 ms - 342[39m [90m[39m
Exception while getting driver instance

Are you sure the path you given is correct ? It looks exactly like mine.

Just try passing these values also,

PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools
PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/platform-tools
PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/tools

14 days later

Prithivi,

For me also same issue ..Not able to start session ..Can u please help me..

Please find the error log below

Error log:
[36minfo[39m: Welcome to Appium v1.3.1 (REV 1160ce02bb89c354cb99317985123acf39f0e7d3)
[36minfo[39m: Appium REST http interface listener started on 127.0.0.1:4723
[36minfo[39m: [debug] Non-default server args: {"address":"127.0.0.1","noReset":true}
Client/4.3.4 (java 1.5)
[36minfo[39m: [debug] Using local app from desired caps: /Users/jagatheswaran/Documents/AppiumPOC/android.apk
[36minfo[39m: [debug] Creating new appium session 5084c368-8b59-4005-92c6-8ee6a1c95116
[36minfo[39m: Starting android appium
[36minfo[39m: [debug] Using fast reset? false
[36minfo[39m: [debug] Preparing device for session
[36minfo[39m: [debug] Checking whether app is actually present
[36minfo[39m: [debug] Checking whether adb is present
[33mwarn[39m: The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb.
[36minfo[39m: [debug] Sent shutdown command, waiting for UiAutomator to stop...
[33mwarn[39m: UiAutomator did not shut down fast enough, calling it gone
[31merror[39m: Failed to start an Appium session, err was: Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
[36minfo[39m: [debug] Cleaning up android objects
[36minfo[39m: [debug] Cleaning up appium session
[36minfo[39m: [debug] Error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
at null. (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-adb/lib/adb.js:113:12)
at ChildProcess.exithandler (child_process.js:652:7)
at ChildProcess.emit (events.js:98:17)
at maybeClose (child_process.js:756:16)
at Socket. (child_process.js:969:11)
at Socket.emit (events.js:95:17)
at Pipe.close (net.js:465:12)
[36minfo[39m: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.)","origValue":"Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path."},"sessionId":null}
[36minfo[39m: [37m<-- POST /wd/hub/session [39m[31m500[39m[90m 7020.937 ms - 342[39m [90m[39m
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find adb. Please set the ANDROID_HOME environment variable with the Android SDK root directory path.) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9.28 seconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'administrators-MacBook-Pro-9.local', ip: '10.0.0.3', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.5', java.version: '1.7.0_60'
Driver info: io.appium.java_client.AppiumDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
at io.appium.java_client.AppiumDriver.(AppiumDriver.java:49)
at asd.main(asd.java:41)

"The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb."

This error will occurs when ANDRIOD_HOME is set, but it was wrong. Check your path variables and try to add "/" in the end like,
"PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools"
into
"PATH=$PATH:$HOME/Downloads/adt-bundle-mac-x86_64/sdk/build-tools/"