Iso
Iso

Reputation: 3758

Eclipse unable to compile ant script programmatically

I have an Ant script that I would like to execute using a button that I made. The problem now is that I would get an error saying:

BUILD FAILED

X:\eclipseMT\runtime-workspace\testTest\diagram1_Sc2_TestScript.xml:68: 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\Java\jdk1.6.0_12\jre"

I'm pretty sure that my classpath is correct and I can execute the same script using Eclipse internal Ant executor.

This is my ant script:

<property name="src.dir" value="src" />
<property name="test.dir" value="test" />
<property name="test.wstest.dir" value="${test.dir}/wstest" />
<property name="junit.dir" location="X:\eclipseMT\plugins\org.junit_3.8.2.v3_8_2_v20100427-1100"/>
<property name="build.dir" value="build" />
<property name="build.classes.dir" value="${build.dir}/classes" />
<property name="build.test.dir" value="${build.dir}/test"/>
<property name="build.test.classes.dir" value="${build.test.dir}/classes" />
<property name="build.test.data.dir" value="${build.test.dir}/data" />
<property name="build.test.reports.dir" value="${build.test.dir}/reports" />
<property name="dist.dir" value="dist" />
<property name="lib.dir" value="lib" />
<property name="build.debug" value="true" />

<path id="compile.classpath">
    <fileset dir="lib">
        <include name="*.jar"/>
    </fileset>
</path>

<path id="test.compile.classpath">
    <path refid="compile.classpath" />
    <pathelement location="${build.classes.dir}"/>
</path>

<path id="test.classpath">
    <path refid="test.compile.classpath" />
    <pathelement location="${build.test.classes.dir}"/>
</path>

<target name="init" description="create dir desc">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${lib.dir}" />
    <copy todir="${lib.dir}">
        <fileset dir="${junit.dir}"/>
    </copy>
    <echo>make init dir done</echo>
</target>

<target name="test-init" depends="init" description="create test dir">
    <mkdir dir="${build.test.dir}" />
    <mkdir dir="${build.test.classes.dir}" />
    <mkdir dir="${build.test.data.dir}"/>
    <mkdir dir="${build.test.reports.dir}"/>
<echo>make test init dir done</echo>
</target>

<target name="clean" depends="init, test-init" description="remove previous build">
    <delete verbose="true">
        <fileset dir="${build.classes.dir}" />
        <fileset dir="${build.test.classes.dir}" />
        <fileset dir="${build.test.data.dir}" />
        <fileset dir="${build.test.reports.dir}" />
        <fileset dir="${dist.dir}" />
    </delete>
    <echo>clean build dir done</echo>
</target>

<target name="compile" depends="clean" description="compile java source">
    <javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpath="${build.classes.dir}" debug="on" fork="no" includeAntRuntime="false" />
    <echo>compile source done</echo>
</target>

<target name="test-compile" depends="compile, test-init" description="compile test source">
    <javac srcdir="${test.wstest.dir}" destdir="${build.test.classes.dir}" debug="true" includeAntRuntime="true">
        <classpath refid="test.compile.classpath" />
    </javac>
    <echo>compile test src done</echo>
</target>

<target name="test-reporting" depends="test-compile" description="report even if fail">
    <junit printsummary="false" errorproperty="test.failed" failureproperty="test.failed">
        <classpath>
            <path refid="test.classpath" />
        </classpath>
        <formatter type="brief" usefile="false" /> 
        <formatter type="xml" />
        <batchtest todir="${build.test.data.dir}" unless="testcase">
            <fileset dir="${build.test.classes.dir}" />
            <!--fileset dir="${build.test.classes.dir}" includes="Sc2TestClient*.class" /-->
        </batchtest>
    </junit>

    <junitreport todir="${build.test.data.dir}">
        <fileset dir="${build.test.data.dir}">
            <include name="WSTEST-*.xml" />
        </fileset>
        <report format="frames" todir="${build.test.reports.dir}" />
    </junitreport>
    <fail if="test.failed">
        Test failed. Check ${build.test.reports.dir}
    </fail>
</target>

<target name="default" depends="test-reporting" description="test the whole suite">
    <echo>all test done</echo>
    <tstamp>
        <format property="buildTime" pattern="yyyy-MM-dd' 'HH:mm:ss" />
    </tstamp>
    <echo>build time = ${buildTime}</echo>
</target>

This is my button code:

Project p = new Project();

    try {
        p.setUserProperty("ant.file", buildFile.getAbsolutePath());     

        DefaultLogger consoleLogger = new DefaultLogger();
        consoleLogger.setErrorPrintStream(System.err);
        consoleLogger.setOutputPrintStream(System.out);
        consoleLogger.setMessageOutputLevel(Project.MSG_INFO);

        p.addBuildListener(consoleLogger);          
        p.fireBuildStarted();
        p.init();

        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, buildFile);
        p.executeTarget(p.getDefaultTarget());
        p.fireBuildFinished(null);
    } catch (Throwable t) {
        t.printStackTrace();
        p.fireBuildFinished(t);
        return false;
    }

Upvotes: 2

Views: 1947

Answers (2)

Iso
Iso

Reputation: 3758

A friend give me a reference to this earlier question: Setting JAVA_HOME when running Ant from Java

The problem basicly is with ant because it has point java.home to 'jre' instead of 'jdk'. It got overwritten everytime ant was call to execute.

The solution is to add:

fork="yes"

to each compilation script and add:

p.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.6.0_26");

to the java execution code.

Hope this help others :D

Upvotes: 0

Nagesh Palathya
Nagesh Palathya

Reputation: 11

It's complaining about javac, unable to find javac.. your JAVA_HOME(C:\Program Files\Java\jdk1.6.0_12\jre) is pointing to only "jre" not to java compiler(javac). Set you JAVA_HOME to "C:\Program Files\Java\jdk1.6.0_12\bin"(which has both javac and jre) and it should work. :-)

--Nagesh Palathya

Upvotes: 1

Related Questions