Start/Kill appium server within java code


#6
public class SampleJavaTest {
private static Process process;
private static String APPIUMSERVERSTART = "node /home/adminuser/Java_Projects/Appium/appium";    

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

public static void stopAppiumServer() throws IOException {
    if (process != null) {
        process.destroy();
    }
    System.out.println("Appium server stop");
}

public static void main(String args[]) throws IOException, InterruptedException {        
    startAppiumServer();
    stopAppiumServer();
}

}


#7
  • You either run it from the terminal or automate the process of that using Java as I mentioned in this post.
  • If you are gonna use the donwnloaded UI Appium, then its location on your system is very relevant as you have to include that location when starting the server. If you are gonna download the Github version of Appium, then the UI Appium is irrelevant in this case and you just need to refer to the location of the Github repository.
  • The answer is there.

#8

Hi Priyank_Shah,
Thanks for the code sample however this is that exact code i used but i am missing the following:

  • What is the path i should give for the runtime.exec()
    Thanks…

#9

Since i am using direct code from my local repo. hence i gave to “node </home/adminuser/Java_Projects/Appium/appium>”. you can use the directory where your appium is stored.
If you had installed appium using brew. then you just need to invoke “appium” command in runtime.exec(APPIUMCOMMAND).

Thanks,
Priyank Shah


#10

Hi Hassan i was able to start the server via terminal as you suggested!!!
i saw posts how to implement it by CommandLine command = new CommandLine(“cmd”);
what is the practice on mac? (i want to start stop the server via code naturally)


#11

On Mac, it should be similar, but you have to change

CommandLine command = new CommandLine(“cmd”);

to be:

CommandLine command = new CommandLine("/bin/sh");


#12

What about the using of environment variables? We can reach agreement about system variable name, unify all drafts above and propose a new pull request.

But firstly…
I have a question. Anybody knows how to get appium node state? Port of the node which is already launched?


#13

I think if you can check using “netstat -anp | grep <appium_port>” using java code.


#14

It needs to think about…

I am thinking about these variants… AndroidDriver and IOSDriver require remote URL. If node has been launched automatically or before test script is started they need to know port. If we could receive actual port number we would use:

new AndroidDriver(Capabilities);
new IOSDriver(Capabilities);

What are you think about it? Above is for the local starting.


#15

I think to remote starting, the same daemon process you need to run (using ssh) that will give you port numbers.


#16

You can use the following command to accomplish that:

echo off & FOR /F “usebackq tokens=2” %a in (TASKLIST ^| findstr /I node.exe) do (FOR /F “usebackq tokens=2” %b in (netstat -nao ^| findstr /R /C:"%a") do echo %b)

Run it from inside your Java application, parse the returned value and get the port number from it :smile:


#17

Thanks for suggestions! I will try it.


#18

@Hassan_Radi
I tried your suggestion for starting appium server through the following java code.
Somehow the server is not starting.When i check using the command sudo lsof -i :4723 there are no process running on the port 4723.
But when i try the same command manually in a terminal without /bin/sh the server starts successfully
Am i missing something here?

I am using Java 8 , Xcode 6.1 , MAC 10.10, Appium 1.3.1

public class Runner {

static String APPIUMCOMMAND = “/bin/sh /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 0.0.0.0 --port 4723”;

public static void main(String[] args) throws ExecuteException, IOException {
startAppiumServer();
}

public static void startAppiumServer() throws ExecuteException, IOException {

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(new CommandLine(APPIUMCOMMAND), resultHandler);
}

Thanks in advance for your time.
Regards Srinidhi


#19

See this for help…


#20

Hi folks first of all thanks for helping me overcome this obstacle.
at the end my solution for starting & stopping the Appium server was simple,
i created 2 shell scripts files, one for starting the server & the other for stopping it.

Start server file include the following data:
/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --no-reset --local-timezone

Stop server file include the following data:
killall node

The java code for starting & Stopping the server was:
//Start Appium server
Runtime.getRuntime().exec("/startAppium");
//Stop Appium server
Runtime.getRuntime().exec("/stopAppium");

Hope it helps future developers!!!
Cheers…


#21

Hi,

I have to start and stop appium server (node) on mac machine using java code from a windows machine

is it possible and if it so can i have the code pls
Thanks in advance

Sugu


#22

Hello All,

Please check the below link from my blog,

Regards,
A. K. Sahu


#23

Hi
I am getting Invalid character error with code 800A03F6. Please help.


#24

Hi Priyank_Shah

I was using this to Start/Stop my Appium Server. It has been working for the past 3 months. But now i upgrade to Appium 1.4.16.1 and this doesn’t start the Appium Server and doesn’t throws any error. Please let me know if I am doing anything wrong.

public class SampleJavaTest {
private static Process process;
public static String APPIUM_NODE_FILEPATH = “C:/Program Files (x86)/Appium/node.exe”;
public static String APPIUM_SERVERSCRIPT_FILEPATH = “C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js”;

public static String APPIUMSERVERSTART = “”" + APPIUM_NODE_FILEPATH + “” “” + APPIUM_SERVERSCRIPT_FILEPATH + “” ";

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

/**

  • DOCUMENT ME!
    */
    public void stopServer()
    {
    if (process != null)
    {
    process.destroy();
    }
    System.out.println(“Appium server stop”);
    }

public static void main(String args[]) throws IOException, InterruptedException {
startAppiumServer();
stopAppiumServer();
}


#25

I don’t have a solution to your problem but you may want to look into the new version of the java-client which supports starting the server from java and offers, as far as I can see, a very elegant solution.