Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Start & Stop Appium Server Programmatically using Java (MAC OSX)
package utilities;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import java.io.IOException;
/**
*
* @author RightQA
*
*/
public class AppiumServer {
public void startServer() {
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("--full-reset", false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(command, resultHandler);
Thread.sleep(5000);
System.out.println("Appium server started.");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void stopServer() {
String[] command = { "/usr/bin/killall", "-KILL", "node" };
try {
Runtime.getRuntime().exec(command);
System.out.println("Appium server stopped.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@benz-philip

This comment has been minimized.

Show comment Hide comment
@benz-philip

benz-philip May 25, 2017

The start and stop methods are good to go with if we have only one instance of appium server. What if we have multiple appium server launched and we want to stop a particular node. using given code to stop server stops all the servers launched.

welcome your suggestions...

The start and stop methods are good to go with if we have only one instance of appium server. What if we have multiple appium server launched and we want to stop a particular node. using given code to stop server stops all the servers launched.

welcome your suggestions...

@Elangopalakrishnan

This comment has been minimized.

Show comment Hide comment
@Elangopalakrishnan

Elangopalakrishnan Jun 6, 2017

I'm also having same question, what we can do if we have 2 appium server instance. @benz-philip

I'm also having same question, what we can do if we have 2 appium server instance. @benz-philip

@Ankit-Pare

This comment has been minimized.

Show comment Hide comment
@Ankit-Pare

Ankit-Pare Jan 23, 2018

When tried to run the above code :
@Test(enabled = true)
public void startServer() {

	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--full-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	try {
		executor.execute(command, resultHandler);
		Thread.sleep(5000);
		System.out.println("Appium server started.");
	} catch (IOException e) {
		e.printStackTrace();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

@Test(enabled = false)
public void stopServer() {
	String[] command = { "/usr/bin/killall", "-KILL", "node" };
	try {
		Runtime.getRuntime().exec(command);
		System.out.println("Appium server stopped.");
	} catch (IOException e) {
		e.printStackTrace();
	}
}

}`

I got the following error :

/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash';
^^^^^^

SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

After running the code :
Got the following message :
Default test
Tests run: 1, Failures: 0, Skips: 0
So the test ran successfully , but after trying to find if any server is working on port 4723 , using
netstat -vanp tcp | grep 4723 , i got no process running on the port.

Can anyone help me with this? TIA :)

Ankit-Pare commented Jan 23, 2018

When tried to run the above code :
@Test(enabled = true)
public void startServer() {

	CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
	command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js", false);
	command.addArgument("--address", false);
	command.addArgument("127.0.0.1");
	command.addArgument("--port", false);
	command.addArgument("4723");
	command.addArgument("--full-reset", false);
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	DefaultExecutor executor = new DefaultExecutor();
	executor.setExitValue(1);
	try {
		executor.execute(command, resultHandler);
		Thread.sleep(5000);
		System.out.println("Appium server started.");
	} catch (IOException e) {
		e.printStackTrace();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

@Test(enabled = false)
public void stopServer() {
	String[] command = { "/usr/bin/killall", "-KILL", "node" };
	try {
		Runtime.getRuntime().exec(command);
		System.out.println("Appium server stopped.");
	} catch (IOException e) {
		e.printStackTrace();
	}
}

}`

I got the following error :

/Applications/Appium.app/Contents/Resources/node_modules/appium/lib/appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash';
^^^^^^

SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:146:18)
at node.js:404:3

After running the code :
Got the following message :
Default test
Tests run: 1, Failures: 0, Skips: 0
So the test ran successfully , but after trying to find if any server is working on port 4723 , using
netstat -vanp tcp | grep 4723 , i got no process running on the port.

Can anyone help me with this? TIA :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment