27
votes

I know that this question is popular, but no one of solutions can help me. I used this, this, this and this solutions, but no one help me.

I want to implement uiautomator Tests and need to build my build.xml with ant, but get this strange error.

I use Windows 8.1

My JAVA_HOME system variable set to c:\programs files\java\jdk1.7.0_51 and to c:\programs files(x86)\java\jdk1.7.0_51

My PATH system variable set also to %JAVA_HOME%/bin, my %ANT_HOME% is set to c:\apache-ant.

But when I execute ant build in the android app derictory I get the

unable to locate tools.jar. Expected find it in C:\Program Files\Java\jre7\lib\tools.jar
BUILD FAILED
Perhaps JAVA_HOME does not point to the JDK

But my JAVA_HOME points to right JDK ! I confused with this. I also rebuild my build several times, I've got the same.

Will be glad if somebody help me.

10
Did you set these variables in PATH variable as well in system environment variables?Smriti
also add %JAVA_HOME%\bin to PATH system variableMandeep Pasbola

10 Answers

39
votes

I too had this problem and solved it by setting variables like this :

[1] ANT_HOME - C:\apache-ant-1.9.3

[2] JAVA_HOME - C:\Program Files\Java\jdk1.7.0_21

[3] PATH - D:\Android_Development\android_sdk\platform-tools\;%ANT_HOME%\bin;%JAVA_HOME%\bin;D:\Android_Development\android_sdk\tools

Note : Set all these in System variables not in user variables.

This solved my problem.

Hope it helps.

18
votes

To Recover this problems you can just copy the tools.jar from "C:\ProgramFiles\Java\jdk1.7.0\lib" directly into "C:\Program Files\Java\jre7\lib\".You will successfully recover the problems.

It works!..

11
votes

I solved issue with setting %JAVA_HOME% to start of the PATH. It worked for me when I set it exactly to the start.

2
votes

In regards to the tools.jar, I experienced the following: when downloading ant, it came with open java 7. However, the system I had downloaded, uses Java 8 u 40 (the Oracle version). Therefore, when I pointed the lib/jre via a softlink in linux, to the place of the tools.jar, I got the error, that now the version/subversion was not matching. This because ant was using version 7, and the tools.jar from Oracle was now version 8.

The solution was, to use the above help (with the setting of the JAVA_PATH to become my Oracle version 8u40 of Java), such that both the compiler and the tools.jar file is of same version.

The scenario I am in, therefore has the jdk downloaded as it came from Oracle in my /home/david/Desktop/Android5.0/jdk1.8.0u40 directory - the Android packages as they came in /home/david/Desktop/Android5.0 directory, my Eclipse workspace in /home/david/workspace - and now I hope to be better to go.

The ant-file build.xml is placed in my /home/david/workspace directory - and it is looking like this:

<project>
    <target name="clean">
        <delete dir="build"/>
    </target>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="oata.HelloWorld"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>
    </target>

</project>

The java file in this question - HelloWorld.java, is being put into the directory /home/david/workspace/build/src/oata - and it now compiles to its destination you can see above, and from there the jar file is being built too.

True - there are methods where one can make one version of Java pretend to be another - but I didn't want to go there, because I am setting up a build environment where I can do extracts from the repository, build, and storing of the built code into a download site.

The above build.xml sample is slightly modified from the following Ant tutorial for build: https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

Guys - hope it works for you too, because ant is really one of the best ways of building, as you can invoke it from the command line, you can make it pick files from the repository first (GitHub or BitBucket etc.), and then you can FTP the compiled result to your preferred download site for developers, you can zip together the code such that you have the version ready and zipped in worst case...

2
votes

Copy the tools.jar from C:\Program Files\Java\jdk1.8.0_91\lib and pate to C:\Program Files\Java\jdk1.8.0_91\bin. It worked fine for me in my case

2
votes
  1. Set in the system-variables:

    • JAVA_HOME: C:\Program Files\Java\jdkVERSION
    • ANT_HOME: C:\Program Files\apache-ant-VERSION
    • PATH: %JAVA_HOME%\bin;%ANT_HOME%\bin;... (at the beginning of the PATH-variable)
  2. Remove everything related to Java/Ant from the user-variables

  3. Restart the commandline (!) to receive the new Variables in the context of the running instance.

0
votes

Check the value of JAVA_HOME and PATH variable, sometime you might be putting

JAVA_HOME="C:\Program Files\Java\jdk1.8.0\bin" .

instead of

JAVA_HOME="C:\Program Files\Java\jdk1.8.0" .

and PATH=%JAVA_HOME%\bin;%PATH% .

just echo out the PATH variable in CLI.

echo %PATH%.
0
votes

kindly set the system variable for JDK not JRE. this will solve the problem

0
votes

Follow the below steps to resolve the issue:

Goto Control Panel -> Set below environment variables:

  1. ANT_HOME - C:\apache-ant-1.9.14

  2. JAVA_HOME - C:\Program Files\Java\jdk1.8.0_181

  3. PATH - %ANT_HOME%\bin;%JAVA_HOME%\bin;

0
votes

This is because editing the environmental variables instead of creating new one. Though java home and path variables were set, it is not pointed properly. Just delete java home and path variable and add it again. This one fixed my issue.