Unable to start appium programitically using Java on Windows OS

Appium v1.6.13
Windows 7

I read through almost all the discussions related to this header, however, I could’t find a solution to the issue I am facing.

I am trying to Launch appium from within my Project using Java. Below is code I am using to launch the appium server

static PropertyFileReader reader = new PropertyFileReader();

static String Appium_Node_Path = reader.nodejsPath();
static String Appium_JS_Path = reader.appiumPath();
static AppiumDriverLocalService service;
static String service_url;
static File file = new File(System.getProperty("user.dir") + "/appium.log");

public static void main(String[] args) throws Exception {
	service = AppiumDriverLocalService
			.buildService(new AppiumServiceBuilder()
			.usingDriverExecutable(new File(Appium_Node_Path))
			.withAppiumJS(new File(Appium_JS_Path))
			.withLogFile(file));
	
	service.start();

        service_url = service.getUrl().toString();
	System.out.println(service_url);
	}
}

Now, I first tried with the path of the appium.js file and faced error in launching appium. Based on the suggestions in the blogs, I replaced the path to the main.js file. Still I get the below exception and am unable to launch the server

Exception in thread “main” io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\Program Files\nodejs\node.exe Arguments: [D:\Users\ASAJATSH\AppData\Roaming\npm\node_modules\appium\lib\main.js, --port, 4723, --address, 0.0.0.0, --log, D:\Users\ASAJATSH\workspace\Go\appium.log]
Process output: D:\Users\ASAJATSH\AppData\Roaming\npm\node_modules\appium\lib\main.js:4
import { init as logsinkInit } from ‘./logsink’;
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions…js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)

at io.appium.java_client.service.local.AppiumDriverLocalService.start(AppiumDriverLocalService.java:155)
at com.Astro.common.Base.AstroBaseTest.main(AstroBaseTest.java:37)

Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://0.0.0.0:4723/wd/hub/status] to be available after 120007 ms
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:104)
at io.appium.java_client.service.local.AppiumDriverLocalService.ping(AppiumDriverLocalService.java:115)
at io.appium.java_client.service.local.AppiumDriverLocalService.start(AppiumDriverLocalService.java:142)
… 1 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)
… 3 more
Caused by: java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
… 4 more

Please help me on this as I am not able to make the reason for this exception. Thanks in Advance.

I’m seeing same issue… any solution for this ?

Same issue here, hoping for a solution.

it has been a while since I resolved this. As far as my memory serves, the issue was with the binaries that I had installed for appium. I had to reinstall the npm and appium package and post restart, the issue was resolved. Hope it helps you guys as well.

I had the same problem, it was solved by specifying 127.0.0.1 address instead of default one (0.0.0.0):

service = AppiumDriverLocalService
		.buildService(new AppiumServiceBuilder()
        .withIPAddress("127.0.0.1");
		.usingDriverExecutable(new File(Appium_Node_Path))
		.withAppiumJS(new File(Appium_JS_Path))
		.withLogFile(file));

Hi Guys,

You can use this complete code to start Appium Programatically;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;

public class AppiumServerJava {

private AppiumDriverLocalService service;
private AppiumServiceBuilder builder;
private DesiredCapabilities cap;

public URL startServer(int port) throws InterruptedException {
	String Appium_Node_Path="C:/Program Files (x86)/Appium/node.exe";
	String Appium_JS_Path="C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js";
	//Build the Appium service
	builder = new AppiumServiceBuilder();
	builder.usingPort(port);
	builder.usingDriverExecutable(new File(Appium_Node_Path));
	builder.withAppiumJS(new File(Appium_JS_Path));
	//Start the server with the builder
	service = AppiumDriverLocalService.buildService(builder);
	service.start();
	return service.getUrl();
}

public void stopServer() {
	service.stop();
}

public boolean checkIfServerIsRunnning(int port) {
	boolean isServerRunning = false;
	ServerSocket serverSocket;
	try {
		serverSocket = new ServerSocket(port);
		serverSocket.close();
	} catch (IOException e) {
		//If control comes here, then it means that the port is in use
		isServerRunning = true;
	} finally {
		serverSocket = null;
	}
	return isServerRunning;
}	

public static void main(String[] args) throws InterruptedException {
	AppiumServerJava appiumServer = new AppiumServerJava();
	int port = 4723;
	if(!appiumServer.checkIfServerIsRunnning(port)) {
		System.out.println(appiumServer.startServer(port));
	} else {
		System.out.println("Appium Server already running on Port - " + port);
	}
}

}