Triggering Appium from the command line via Java code

@Alexis Thank you very much for your reply. In my current code itself I have used
Thread.sleep(30000); [30seconds]
But still the issue is exist.

Any suggestions @Hassan_Radi.

Please give this a try and let me know how it works with you: https://github.com/Genium-Framework/Appium-Support/wiki

I tried with the Appium support library:

CODE:

File file = new File(“D:/programfiles”);

ServerArguments serverArguments = new ServerArguments();

serverArguments.setArgument(“–address”,“127.0.0.1”);
serverArguments.setArgument(“–chromedriver-port”, 9516);
serverArguments.setArgument(“–bootstrap-port”, 4725);
serverArguments.setArgument(“–no-reset”, true);
serverArguments.setArgument(“–local-timezone”, true);

AppiumServer appiumServer = new AppiumServer(file, serverArguments);
appiumServer.startServer();

System.out.println(“started”);

CONSOLE:
Aug 12, 2015 10:41:35 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…

After that nothing is happening

Appium Installation Directory
node.exe= D:\programfiles\node.exe
appium.js= D:\programfiles\node_modules\appium\bin\appium.js

this line should cause a compile time error as there is no such constructor.

Second issue is that the constructor doesn’t take a File object, it takes a String representation of the path.

Please change the whole line with the following line, give it a try and let me know how it works:

AppiumServer appiumServer = new AppiumServer(serverArguments, “D:/programfiles”);

I have tried that but it gives me an error like

With the file object

Passing as a String

Please check my updated post above

I have tried with the string representation also and updated the screenshot in the previous post. PFA

This is the constructor you are trying to invoke: https://github.com/Genium-Framework/Appium-Support/blob/master/src/main/java/com/github/genium_framework/appium/support/server/AppiumServer.java#L112

With reference to the Constructor only I passed like

AppiumServer appiumServer = new AppiumServer(file, serverArguments);

But when changing the code line like

AppiumServer appiumServer = new AppiumServer(serverArguments,file);
OR
AppiumServer appiumServer = new AppiumServer(serverArguments,“D:/programfiles”);

It says it is undefined.

Do I miss some thing here?

Sorry for the confusion, it was a documentation issue and it is fixed now.

This is the correct usage of the constructor. Is it giving you any errors??

It is not giving any errors. Once the appiumServer.startServer(); is initiated.

It is not displaying any information in the console.Apart from this below Console log

Aug 12, 2015 11:13:27 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…

ok… if it failed to start the server within 30 seconds it should throw a timeout exception. Please let it run for that amount of time and show me the exception details.

I run the code and waited for 5 minutes. Till now I didn’t receive any exceptions in the console log.

In the code after the appiumServer.startServer(); method I have added the System.out.println(“Server started”);

Even it is not moving to the next line.

Now, I purposely made some wrong changes in the code like

File file = new File(“D:/programfiles”);

ServerArguments serverArguments = new ServerArguments();

serverArguments.setArgument(“address”,“127.0.0.1”); [removed the ‘–’]
serverArguments.setArgument(“–chromedriver-port”, 9516);
serverArguments.setArgument(“–bootstrap-port”, 4725);
serverArguments.setArgument(“–no-reset”, true);
serverArguments.setArgument(“–local-timezone”, true);

AppiumServer appiumServer = new AppiumServer(file, serverArguments);
appiumServer.startServer();

System.out.println(“started”)

When I run the above code I get an exception like:

SUCCESS: The process “node.exe” with PID 5824 has been terminated.
Appium server is starting
Aug 12, 2015 11:28:56 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server is starting…
Aug 12, 2015 11:28:56 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:57 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:58 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:28:59 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:01 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:02 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:03 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:04 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:05 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:06 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:07 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:08 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:09 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:11 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:12 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:13 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:14 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:15 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:16 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:17 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:18 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:19 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:20 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:22 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:23 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:24 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Aug 12, 2015 11:29:25 AM com.github.genium_framework.appium.support.server.AppiumServer startServer
INFO: Server has not started yet. Trying again in one second…
Exception in thread “main” com.github.genium_framework.server.exception.ServerTimeoutException: The server didn’t start after 30 seconds with error:
usage: appium.js [-h] [-v] [–shell]
[–localizable-strings-dir LOCALIZABLESTRINGSDIR] [–app APP]
[–ipa IPA] [-U UDID] [-a ADDRESS] [-p PORT]
[-ca CALLBACKADDRESS] [-cp CALLBACKPORT] [-bp BOOTSTRAPPORT]
[-k] [-r BACKENDRETRIES] [–session-override] [–full-reset]
[–no-reset] [-l] [-lt LAUNCHTIMEOUT] [-g LOG]
[–log-level {info,info:debug,info:info,info:warn,info:error,warn,warn:debug,warn:info,warn:warn,warn:error,error,error:debug,error:info,error:warn,error:error,debug,debug:debug,debug:info,debug:warn,debug:error}]
[–log-timestamp] [–local-timezone] [–log-no-colors]
[-G WEBHOOK] [–native-instruments-lib]
[–app-pkg ANDROIDPACKAGE] [–app-activity ANDROIDACTIVITY]
[–app-wait-package ANDROIDWAITPACKAGE]
[–app-wait-activity ANDROIDWAITACTIVITY]
[–android-coverage ANDROIDCOVERAGE] [–avd AVD]
[–avd-args AVDARGS]
[–device-ready-timeout ANDROIDDEVICEREADYTIMEOUT] [–safari]
[–device-name DEVICENAME] [–platform-name PLATFORMNAME]
[–platform-version PLATFORMVERSION]
[–automation-name AUTOMATIONNAME]
[–browser-name BROWSERNAME] [–default-device]
[–force-iphone] [–force-ipad] [–language LANGUAGE]
[–locale LOCALE] [–calendar-format CALENDARFORMAT]
[–orientation ORIENTATION]
[–tracetemplate AUTOMATIONTRACETEMPLATEPATH]
[–instruments INSTRUMENTSPATH] [–show-sim-log]
[–show-ios-log] [–nodeconfig NODECONFIG] [-ra ROBOTADDRESS]
[-rp ROBOTPORT] [–selendroid-port SELENDROIDPORT]
[–chromedriver-port CHROMEDRIVERPORT]
[–chromedriver-executable CHROMEDRIVEREXECUTABLE]
[–use-keystore] [–keystore-path KEYSTOREPATH]
[–keystore-password KEYSTOREPASSWORD] [–key-alias KEYALIAS]
[–key-password KEYPASSWORD] [–show-config]
[–no-perms-check] [–command-timeout DEFAULTCOMMANDTIMEOUT]
[–keep-keychains] [–strict-caps] [–isolate-sim-device]
[–tmp TMPDIR] [–trace-dir TRACEDIR]
[–intent-action INTENTACTION]
[–intent-category INTENTCATEGORY]
[–intent-flags INTENTFLAGS]
[–intent-args OPTIONALINTENTARGUMENTS]
[–dont-stop-app-on-reset] [–debug-log-spacing]
[–suppress-adb-kill-server] [–async-trace]

appium.js: error: Unrecognized arguments: address 127.0.0.1.

The error was correctly thrown at the console.

Now I added the ‘–’ infront of the address and I run the code.

No exceptions thrown in the screen.

In the File Object do we need to pass node.exe path or appium.js path
Because in command line arguments I will pass both of the directory path.

no, you don’t need to pass any of these paths. can you please check if you have a process running that is called “node.exe” after running the same code.

Also please can you provide your OS details, Appium server version and JDK version.

Yes when running the code the node.exe process is started. PFA

Java version
java version “1.7.0_51”
Java™ SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot™ 64-Bit Server VM (build 24.51-b03, mixed mode)

OS: Windows 7 64 bit

Appium Server: 1.4.0.0

@Hassan_Radi Today morning I run the same code and that code started to run. But when creating an android driver for the 127.0.0.1:4725 it fails.

Either it is not running or it is failing with refused to connect with 127.0.0.1:4725 because of Android driver

Sorry for the later reply, I was investigating the cause of the issue you are experiencing. The underlying code that is responsible for interacting with the Appium server uses Java’s Process class which has a limitation on some operating systems including windows. This limitation causes the spawned process to get stuck into a deadlock state preveting interaction with it and thus causing the unresponsiveness you are experiencing. I am working on a solution to this issue and will release a new version of the library that fixes this problem.

Can you elaborate more?? Did the server instance open successfully and you are having issues connecting to it? If this is the case, please attache your code and logs.