Package name is different than the folder structure but still Java code compiles

The Java language specification doesn’t force files to be in a certain directory. It optionally allows the compiler to require that public classes are in files with the same name of the class, but I don’t think there’s anything similar for packages. Section 7.2.1 talks about possible storage options in a file system, but it doesn’t say anything about enforcing source code structure, as far as I can see.

However, it’s best practice – and a pretty much universally accepted convention – to reflect the package structure in the source directory structure… and javac will use this to try to find source files which aren’t explicitly specified to be compiled.

Note that if you’re compiling from the command line, by default each class will appear in the same location as the corresponding source file, but if you use the “-d” option (e.g. “-d bin”) the compiler will build an appropriate output directory structure for you, rooted in the specified directory.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)