Thursday 10 January 2013

Setting Eclipse JDK

My problem

Recently I was attempting to use an Eclipse plugin that called an Ant build.xml (this ant build subsequently called a Maven task, but that's not too relevant here).  This ant build produced a very simple error (or so I thought), but this simple error saw at least 4 hours go down the drain.


My error in Eclipse, everything fine from the Windows 7 command prompt:
     [java] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.4:compile (default-compile) on project MyTest: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jre7\..\lib\tools.jar -> [Help 1]


Searching for a solution (ask Google approach)

All the forums, Stackoverflow, JavaRanch, etc all suggest I simply set Java -> Installed JREs to a JDK, or put tools.jar in the ant classpath and that will resolve the problem...  Not so lucky, here's a short account of how I resolved this issue.  I hope this saves someone some time.


http://www.coderanch.com/t/105280/vc/set-jdk-version-eclipse
http://stackoverflow.com/questions/8749557/how-do-i-configure-jdk-for-eclipse
http://stackoverflow.com/questions/1288343/how-to-change-java-home-for-eclipse-ant
http://stackoverflow.com/questions/7321691/eclipse-ant-jdk-issue
http://www.dynamicobjects.com/d2r/archives/002591.html



My configuration

C:\ >echo %JAVA_HOME%
C:\Java\jdk1.7.0_07

I only have JDK’s installed in my eclipse (Window -> Preferences -> Java -> Installed JREs):



Debugging the problem

Debugging the problem was not quite as easy as it sounds.  I found the following "echoproperties" task to be the most useful technique.  When initially trying to run this I ran into a separate issue as Eclipse doesn't seem to fully change the Ant version when you set the Eclipse Ant home, but that's surely a separate problem and a separate blog. 

Added this little snippet to our build.xml:
    <taskdef name="echoproperties" classname="org.apache.tools.ant.taskdefs.optional.EchoProperties">
        <classpath>
            <fileset dir="${env.ANT_HOME}/lib">
                <include name="ant.jar" />
            </fileset>
        </classpath>
    </taskdef>
   <echoproperties prefix="java"/>
    <echoproperties prefix="ant"/>


Now for the solution

Seems that Eclipse sets the java.home property relative to the javaw executable.  Then ant attempts to use the java.home variable to find the tools.jar.  Very odd.

C:\>where javaw.exe
C:\Windows\System32\javaw.exe
C:\Java\jdk1.7.0_07\bin\javaw.exe

Run Eclipse
C:\>C:\eclipse\eclipse\eclipse.exe -data c:\eclipse\workspaces\interaction

[echoproperties] java.home=C\:\\Program Files\\Java\\jre7



Put JAVA_HOME right at the beginning of the PATH
C:\>set PATH=%JAVA_HOME%\bin;%PATH%

C:\>where javaw.exe
C:\Java\jdk1.7.0_07\bin\javaw.exe
C:\Windows\System32\javaw.exe

Run Eclipse (again)
C:\>C:\eclipse\eclipse\eclipse.exe -data c:\eclipse\workspaces\interaction

[echoproperties] java.home=C\:\\Java\\jdk1.7.0_07\\jre


Now it will work.

To use my plugin without any odd specification of the ant classpath, %JAVA_HOME%\bin needs to be the first thing in the path.

No comments:

Post a Comment