No error if source directory and package doesn't match

Hello,

I am using Sun's jdk1.5.0_09 "javac" compiler and the package name in my Test class is written as "com.hrishijoshi.asdf". This file compiles even if I put it in a directory called "com" (or anything else, as a matter of fact).

javac doesn't throw error even if the source file's directory structure doesn't match the package name defined in that file! It creates a directory structure matching the package name (not the source tree) in the output "classes" directory.

Is this a feature or a bug? ;-)

- Hrishi Joshi.

PS: Here is a log of my compilation attempt on Fedora Core 5 Linux:

[hrishi@hrishi test]$

[hrishi@hrishi test]$ rm -rf classes/com/

[hrishi@hrishi test]$ cat src/com/Test.java

package com.hrishi.asdf;

publicclass Test{

}

[hrishi@hrishi test]$ javac -Xlint:all -sourcepath src -d classes src/com/Test.java

[hrishi@hrishi test]$

[hrishi@hrishi test]$ tree classes

classes

`-- com

`-- hrishi

`-- asdf

`-- Test.class

3 directories, 1 file

[hrishi@hrishi test]$

[1271 byte] By [HrishiJoshia] at [2007-11-27 2:57:21]
# 1

There is no requirement that source code files are in the correct directory structure. If you want javac to be able to automatically compile dependent files, then the source code must be in an appropriate structure and find-able from the classpath.

Also, when you use the -d option, javac will create an appropriate directory relative to the directory specified in the -d option. That's how it is supposed to work.

So it is working as designed and specified.

atmguya at 2007-7-12 3:35:48 > top of Java-index,Developer Tools,Java Compiler...