Why I can run appium.js in terminal but failed using java with error like "error=2, No such file or directory"

ENV:
MAC10.11
Appium 1.4.13

I want to start Appium server using command in java.
Code:

CommandLine cl=CommandLine.parse(“appium.js”);
cl.addArgument("-U814d53f77e5c5ecf2eca376ae3696dffa692cec8");
System.out.println(cl.getExecutable());
DefaultExecutor executor=new DefaultExecutor();
executor.setWorkingDirectory(new File("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin"));
int i=executor.execute(cl);
System.out.println(i);

Log: TestNG Trace
java.io.IOException: Cannot run program “appium.js” (in directory “/Applications/Appium.app/Contents/Resources/node_modules/appium/bin”): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
at com.rachel.sametimeauto.appium.utilities.AppiumService.startAppium(AppiumService.java:25)
at com.rachel.sametimeauto.appium.iOS.MeetingsTest.iOSMeetingsTVTSanityTest.setUp(iOSMeetingsTVTSanityTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:170)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
… 32 more

Could anyone please help me out? Why it can’t find the file…? Thank you very much!

You can easy start appium 1.5 programmatically with client 3.4.0.
Check topic below:

I run it on Mac using the following parameters.
Working directory: /Applications/Appium.app/Contents/Resources/node_modules/appium
Program: /Applications/Appium.app/Contents/Resources/node/bin/node
Arguments: lib/server/main.js

Could you please give me more details, how can I use these parameters? thank you! @deadmoto

I can’t find these arguments in appium api reference~~ could you please advise?

However, I changed my code to below: public void startServer() throws Exception{
service=AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("/Applications/Appium.app/Contents/Resources/node/bin//node"))
.withAppiumJS(new File("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin//appium.js"))
.withIPAddress(“127.0.0.1”)
.usingPort(4723));
IOSDriver driver=getIOSDriver();
}

IOSDriver getIOSDriver() throws Exception {
	 String path=System.getProperty("user.dir");
	 String file=path+File.separator+"ConfigInfo.xml";
	 log.info("Configuration XML path= "+file+" reading xml begin...");	        
	 /**********For device*********/
	 DesiredCapabilities capabilities = new DesiredCapabilities();
	 capabilities.setCapability(CapabilityType.BROWSER_NAME, getElement.getXMLElement("iOStype", file));
	 capabilities.setCapability("platformName", getElement.getXMLElement("platformname", file));
	 capabilities.setCapability("deviceName", getElement.getXMLElement("devicename", file));
	 capabilities.setCapability("platformVersion", getElement.getXMLElement("platformversion", file));
	 capabilities.setCapability("appium-version", getElement.getXMLElement("appiumversion", file));
	 capabilities.setCapability("app",getElement.getXMLElement("iOSdevicemtbuildpath", file));
	 //capabilities.setCapability("language","es");
	 // support Chinese
	 capabilities.setCapability("unicodeKeyboard", "True");
	 capabilities.setCapability("resetKeyboard", "True");
	 //driveriOS = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
	 driveriOS=new IOSDriver(service,capabilities);

	return driveriOS;
}

This can start successfully~~~