Java ant Eclipse run error [duplicate]

旧城冷巷雨未停 提交于 2019-12-23 19:07:16

问题


Possible Duplicate:
ant error JAVA_HOME does not point to SDK

I'm getting the following error:

BUILD FAILED
C:\Users\myname\Documents\CMSC\Proj1\build.xml:22: 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.
It is currently set to "C:\Program Files (x86)\Java\jre7"

Total time: 1 second

My build.xml file contains the following code:

<project name="Project1" default="compile" basedir=".">

  <description>
    Build file for Project1
  </description>

  <!-- global properties for this build file -->
  <property name="source.dir" location="src"/>
  <property name="build.dir" location="bin"/>
  <property name="doc.dir" location="doc"/>
  <property name="main.class" value="proj1.Project1"/>

  <!-- set up some directories used by this project -->
  <target name="init" description="setup project directories">
    <mkdir dir="${build.dir}"/>
    <mkdir dir="${doc.dir}"/>
  </target>

  <!-- Compile the java code in ${src.dir} into ${build.dir} -->
  <target name="compile" depends="init" description="compile java sources">
    <javac srcdir="${source.dir}" destdir="${build.dir}"/>
  </target>

  <!-- execute the program with the fully qualified name in ${build.dir} -->
  <target name="run" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
  </target>

  <!-- Delete the build & doc directories and Emacs backup (*~) files -->
  <target name="clean" description="tidy up the workspace">
    <delete dir="${build.dir}"/>
    <delete dir="${doc.dir}"/>
    <delete>
      <fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
    </delete>
  </target>

  <!-- Generate javadocs for current project into ${doc.dir} -->
  <target name="doc" depends="init" description="generate documentation">
    <javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
  </target>

</project>

What am I doing wrong here? It appears that it's trying to find a javac file in my src folder but I don't know how to fix that.


回答1:


The JAVA_HOME must be pointed to JDK and not JRE.

To test it, before executing ant type set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_07 or whatever version you have.




回答2:


Download the latest JDK here. Install it and notice where it goes. For example, the 32-bit JDK should install to C:\Program Files (x86)\Java\jdkSOMETHING.

In Eclipse, go to Window > Preferences > Java > Installed JREs and click Add > Standard JVM (then Next). Browse to the directory noted above for the JDK and then hit Finish. To check that your project is using this JDK, right-click the project and go to Properties. From there, look for Java Build Path. Under the Libraries tab you should see a JRE System Library. If it's not the JDK you just added, click the entry and hit the Edit button and change it.

Now run your Ant build.




回答3:


Looking at your error, looks like its Windows environment and you are running ant through command prompt.

Follow Right Click(My Computer) -> properties -> Advanced 
-> Environment Variables -> System Variables -> New
Add  Variable Name = JAVA_HOME
     Variable Value = Path to your Base folder of Java 
                      e.g. C:\Program Files\Java\jdkxxx

Restart the command prompt, type java -version. If it prints your java version correctly, you should be all set.




回答4:


it is saying

"Perhaps JAVA_HOME does not point to the JDK."

Perhaps you should check that :D



来源:https://stackoverflow.com/questions/12806700/java-ant-eclipse-run-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!