Bug 43794 - Message about JAVA_HOME is confusing
Summary: Message about JAVA_HOME is confusing
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.7.0
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-04 23:55 UTC by Dr Russel Winder
Modified: 2007-12-17 23:36 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dr Russel Winder 2007-11-04 23:55:55 UTC
When using the AntBuilder in Groovy, the following error message occurs when
trying to use the Javac task:

Caught: : 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 "/usr/lib/jvm/java-6-sun-1.6.0.03/jre"
        at GROOVY_2258.run(GROOVY_2258.groovy:5)
        at GROOVY_2258.main(GROOVY_2258.groovy)

The Ant problem here is that the JAVA_HOME mentioned here is not the user's
environment variable JAVA_HOME.  Instead I believe it relates to the variable
JAVA_HOME in src/main/org/apache/tools/ant/util/JavaEnvUtils.java.  Although
there is a relationship between the paths involved, the variable JAVA_HOME is
more directly related to System.getProperty ( "java.home" ) than to
System.getenv ( "JAVA_HOME" ) -- the former refers to the JRE whereas the latter
 will point a directory higher.

Clearly this is not an issue to the correct execution on Ant in and of itself.
However for any system (such as Groovy) that uses the Ant jars programmatically,
this is actually a real problem.
Comment 1 Bryan Taylor 2007-12-17 23:36:35 UTC
The error message in question is produced in
org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(String
compilerType, Task task) after Javac.execute() calls compile() which calls it.

The search in getCompiler(..) is compiler type specific, but it looks like you
are running java-6 on linux or unix, so you get the common case where it calls
doesModernCompilerExist() and if this returns false, it throws the error.
Interestingly, this method doesn't appear to use either java.home or JAVA_HOME
directly. Instead it tries to load it in two ways, returning false if both fail
 - Class.forName(MODERN_COMPILER); 
 - CompilerAdapterFactory.class.getClassLoader().loadClass(MODERN_COMPILER)
where MODERN_COMPILER = "com.sun.tools.javac.Main"

I would think those two are usually equivalent, so the question is how
CompilerAdapterFactory.class (and presumably Javac.class) could get loaded in a
classloader that doesn't have tools.jar on it's classpath.

The message should just say something like "Can't find com.sun.tools.javac.Main
on the classpath that loaded the javac task. Make sure tools.jar from the JDK is
on the classpath." It seems like the bootclasspath and classpath task attributes
could also be used to supplement the search.