Ant build executes cordova

2019-07-17 21:58发布

I created an ant build for my cordova project as following:

<project default="build">
    <target name="init-android">
        <exec executable="cordova">
            <arg value="platform"/>
            <arg value="add"/>
            <arg value="android"/>
        </exec>
        <exec executable="cordova">
            <arg value="build"/>
        </exec>
    </target>
</project>

But I got this error:

C:\path_to_project\build.xml:3: Execute failed: java.io.IOException: Cannot run program "cordova": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) at java.lang.Runtime.exec(Runtime.java:615) at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav a13CommandLauncher.java:41)

I can run cordova command with no problem from the command prompt, I have:

JAVA_HOME = C:/Program Files/Java/jdk1.7.0_10/ ANT_HOME = C:\Program Files\Java\apache-ant-1.9.2 NODEJS_HOME = C:\Program Files\nodejs

and they are all in my path. I don't understand why it doesn't work. Please help. Thanks

1条回答
祖国的老花朵
2楼-- · 2019-07-17 22:14

Generally when working with a Java application to launch programs in Windows, I often have to execute cmd.exe and pass it the full path to the program I actually want to run. This allows system environment variables and such to be set up the way you expect. Try this:

<project default="build">
    <target name="init-android">
        <exec executable="cmd.exe">
            <arg value="/C"/>
            <arg value="cordova"/>
            <arg value="platform"/>
            <arg value="add"/>
            <arg value="android"/>
        </exec>
        <exec executable="cmd.exe">
            <arg value="/C"/>
            <arg value="cordova"/>
            <arg value="build"/>
        </exec>
    </target>
</project>

If that still doesn't work, give the full path for cordova. An environment variable should work if you have one defined.

查看更多
登录 后发表回答