简体   繁体   中英

Why can't Ant find javac?

There is the following build.bat file:

echo off  
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_25"  
set BUILD_CP="C:\Program Files\Java\jdk1.6.0_25\bin\lib\tools.jar";"C:\Program Files\Java\jdk1.6.0_25\bin";..\devlib\ant.jar;..\devlib\optional.jar;..\devlib\junit.jar;..\devlib\xercesImpl.jar;..\devlib\xmlParserAPIs.jar;  
"C:\Program Files\Java\jdk1.6.0_25\bin\java" -classpath %BUILD_CP% org.apache.tools.ant.Main -buildfile build.xml all  

set BUILD_CP=  

pause  

When I run it I get:

BUILD FAILED

file:D:/Development/Java/Frameworks/JMeter/TestDemoIbatis/iBATIS_JPetStore-4.0.5/build/build.xml:29: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK

Total time: 0 seconds
Press any key to continue . . .  

But my JAVA_HOME , PATH is properly set :

在此处输入图片说明

Update:
The ant script:

<project name="JPetStore" default="all" basedir=".">  

  <property file="build.properties"/>  

  <path id="classpath">  
    <pathelement location="${src}/"/>  
    <fileset dir="${lib}" includes="**/*.jar" />  
    <fileset dir="${devlib}" includes="**/*.jar" />  
  </path>  

  <target name="clean" >  
    <delete dir="${wars}"/>  
    <delete dir="${webapp}"/>  
    <delete>  
      <fileset dir="${src}" >  
        <include name="**/*.class"/>  
      </fileset>  
    </delete>  
  </target>  

  <target name="prepare" depends="clean">  
    <mkdir dir="${wars}"/>  
    <mkdir dir="${webapp}"/>  
    <mkdir dir="${webapp}/WEB-INF/classes"/>  
    <mkdir dir="${webapp}/WEB-INF/lib"/>  
  </target>  

  <target name="compile" depends="prepare"> //LINE 29 that fails 
    <javac srcdir="${src}" destdir="${webapp}/WEB-INF/classes" deprecation="off" debug="${debug}">  
      <classpath refid="classpath"/>  
    </javac>  
  </target>  

  <target name="assemble.view">  
    <copy todir="${webapp}">  
      <fileset dir="${web}">  
        <include name="**/*.jsp"/>  
        <include name="**/*.html"/>  
      </fileset>  

    </copy>  
  </target>  

  <target name="assemble" depends="compile">  
    <copy todir="${webapp}/WEB-INF/classes">  
      <fileset dir="${src}" >  
        <exclude name="**/*.java"/>  
        <exclude name="**/*.class"/>  
      </fileset>  
    </copy>  
    <copy todir="${webapp}/WEB-INF/lib">  
      <fileset dir="${lib}" />  
    </copy>  
    <copy todir="${webapp}">  
      <fileset dir="${web}" />  
    </copy>  
  </target>  

  <target name="war" depends="assemble">  
    <jar jarfile="${wars}/jpetstore.war">  
      <fileset dir="${webapp}">  
        <include name="**/*"/>  
      </fileset>  
    </jar>  
  </target>  

  <target name="all" depends="war" />  

</project>  

What is the problem here? I am in Windows7/64-bit

Try putting double quotes around the classpath to prevent the classpath being split up due to the spaces:

From this:

"C:\Program Files\Java\jdk1.6.0_25\bin\java" -classpath %BUILD_CP% org.apache.tools.ant.Main -buildfile build.xml all  

To

"C:\Program Files\Java\jdk1.6.0_25\bin\java" -classpath "%BUILD_CP%" org.apache.tools.ant.Main -buildfile build.xml all 

Edit:

You could also try removing the quotes from the BUILD_CP.

echo off  
set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_25"  
set BUILD_CP=C:\Program Files\Java\jdk1.6.0_25\bin\lib\tools.jar;C:\Program Files\Java\jdk1.6.0_25\bin;..\devlib\ant.jar;..\devlib\optional.jar;..\devlib\junit.jar;..\devlib\xercesImpl.jar;..\devlib\xmlParserAPIs.jar;  
"C:\Program Files\Java\jdk1.6.0_25\bin\java" -classpath "%BUILD_CP%" org.apache.tools.ant.Main -buildfile build.xml all  

set BUILD_CP=  

pause  

Seems to be some sort of issue with the spaces in Program Files .
Following @Chris comment I tried to build the file myself and noticed that ant -p failed due to "" in the environmental variables.
By making JAVA_HOME=C:\\Program Files\\Java\\jdk1.6.0_25 from JAVA_HOME="C:\\Program Files\\Java\\jdk1.6.0_25" I was able to run ant directly and build succesfully.
I never had issues with spaces in JAVA_HOME and I always install it in Program Files .
So I am not sure if this is some issue of Windows 7 .
If anyone could explain why this occurs I will mark the answer as accepted

This just worked for me:

echo off

set JAVA_HOME="c:\Program Files\Java\jdk1.6.0_25"

set BUILD_CP=%JAVA_HOME%\lib\tools.jar;..\devlib\ant.jar;..\devlib\optional.jar;..\devlib\junit.jar;..\devlib\xercesImpl.jar;..\devlib\xmlParserAPIs.jar;

%JAVA_HOME%\bin\java -classpath %BUILD_CP% org.apache.tools.ant.Main -buildfile build.xml all

set BUILD_CP=

pause

Note that I had to change the build.xml to add the source=1.4 directive:

<javac srcdir="${src}" destdir="${webapp}/WEB-INF/classes" deprecation="off" debug="${debug}" source="1.4">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM