tags:

views:

20

answers:

3

I start studying ANT today in order to make Java compiling easier. I wrote simple ANT script which only use javac command.

What I am trying is compile from .....\head_first\src\com\masatosan\constant.java (source)

to the destination directory:

.....\head_first\WEB-INF\classes\com\masatosan\conf

So the result would look like:

.....\head_first\WEB-INF\classes\com\masatosan\conf\constant.class

But I can't figure out why the actual result adds "/com/masatosan/conf" directories to the destination folder, so it looks like:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\WEB-INF\classes\com\masatosan\conf\com\masatosan\constant.class

Could anyone tell me how can I fix this?

ANT

<project name="CompileMasatosan" 
    basedir="C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\src\com\masatosan">
    <description>
        masatosan compiler
    </description>
    <property name="confSrc" 
        location="C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\src\com\masatosan\conf" />
    <property name="confDest"
        location="C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\WEB-INF\classes\com\masatosan\conf" />

    <target name="compileConfSrc">
        <javac srcdir="${confSrc}" destdir="${confDest}" />
    </target>
</project>

UPDATE

I didn't know complier creates directories based on the package name.

The package name of constant.java was com.masatosan.conf so that complier creates "/com/masatosan/conf/"

+1  A: 

This is because the compiler is creating the package directory stucture under the destination directory you specify. The constant class is in the com.masatosan package, so under classes\com\masatosan\conf, the compiler creates a further two directories for the package, com\masatosan, and places the compiled class (constant.class) in there.

Your destination directory should simply be C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\WEB-INF\classes. The com and masatosan directories will be created for you.

Richard Fearn
+2  A: 

Hi masato-san,

Its because of the package name. You have a Java file in package com.masatosan. You can fix this two way either move class to default package or set ${confDest} value to \head_first\WEB-INF\classes\ only.

Jprogyog
+2  A: 

You're trying to tell ANT to change the package because you want Constant.class to be under the com.masatosan.conf package rather than the com.masatosan package. The Ant compilation process will create the appropriate package subdirectories, which is why you see com\masatosan created under the dest.

I don't think you can tell ANT to change the package of a source file, which is what you're trying to do by injecting an extra conf dir. You can either create a conf dir and have a conf\com\masatosan\constants.class or put it under classes\com\masatosan\constants.class, but you can't do com\masatosan\conf\constants.class since that changes the package of constants.class to com.masatosan.conf

Or simply change the package of constants.class to com\masatosan\conf and change your ant file to to:

<property name="confDest" location="C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\head_first\WEB-INF\classes" />

SB
Thank you for the tip guys! But how does complier know that it should create exactly "/com/masatosan/conf" for me instead of, say, "head_first/com/masatosan/conf"? Oh... is it because package name of constant.java is "com.masatosan.conf" ?
masato-san
I got this answer :) http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html
masato-san