Appium starting programmatically through code in mac

I have used the following snippet for launching the appium server through java code in mac.

public void startAppium() throws ExecuteException, IOException, InterruptedException {
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);
Thread.sleep(50000);
System.out.println(“Appium Server starting”);
}

I’m calling the above method inside @BeforeTest. Inside @BeforeMethod I’m mentioning the desired capabilities. The server launching and it is taking the desired capabilities as well but it is showing the following log with error:

e[36minfoe[39m: e[37m–>e[39m e[37mPOSTe[39m e[37m/wd/hub/sessione[39m e[90m{“desiredCapabilities”:{“app”:"/Users/aaaa/dd/qd.apk",“appPackage”:“com.xxx.xxx”,“appActivity”:“com.xxx.xxx.MainActivity”,“newCommandTimeout”:“1200”,“platformName”:“Android”,“deviceName”:“Android Device”}}e[39m
e[36minfoe[39m: Client User-Agent string: Apache-HttpClient/4.5.2 (Java/1.8.0_73)
e[36minfoe[39m: [debug] Using local app from desired caps: /Users/aaaa/dd/qd.apk
e[36minfoe[39m: [debug] Creating new appium session 6a2245bd-d1ac-443c-bf5f-c6e2c2f64e22
e[36minfoe[39m: Starting android appium
e[36minfoe[39m: [debug] Getting Java version
e[36minfoe[39m: [debug] Cleaning up android objects
e[36minfoe[39m: [debug] Cleaning up appium session
e[31merrore[39m: Failed to start an Appium session, err was: Error: ‘java -version’ failed. Error: Command failed: /bin/sh -c java -version
/bin/sh: java: command not found

e[36minfoe[39m: [debug] Error: ‘java -version’ failed. Error: Command failed: /bin/sh -c java -version
/bin/sh: java: command not found

I have tried by setting the JAVA_HOME to environment variable of the project but it is of no use.

Can anyone please provide the suggestion on this.Thanks in advance.

Whatever IDE you are using doesn’t know where Java is installed. You should look at the documentation for that IDE on how to set up a Java project.

I think you installed all the required packages using brew .

Use the below command to start Appium .

	public void startServer() {
 
		CommandLine command = new CommandLine(
				"/usr/local/bin/node");
		command.addArgument(
				"/usr/local/bin/appium",
				false);
		DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
		DefaultExecutor executor = new DefaultExecutor();
		executor.setExitValue(1);
		try {
			executor.execute(command, resultHandler);
			Thread.sleep(10000);
			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();
		}
	}