Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Notepad++ to write my Java code and Command Prompt to compile and run it. Following is my sample Java code,

    package abraKadabra;      public class SuperClass{        protected int anInstance;         public static void main(String [] abc){            System.out.println("Hello");        }     } 

However, this file is in the following folder structure :

"usingprotected\superPkg" (usingProtected is a folder somewhere in the hierarchy in C:)

So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.

But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure? And if it should, how would it adhere?

For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?

like image 626
whitehat Avatar asked Dec 06 '11 05:12

whitehat


People also ask

Do Java package names matter?

Naming ConventionsPackage names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .

Is package and folder same in Java?

In the end, a package (in the java's POV) is a collection of classes and subpackages, that eventually will be the core structure of an application or library. In the other way, a JAVA package (in the IDE POV) is a normal folder that contains all it's . class or . java files.

Can package name be same as class name in Java?

Yes, it will work.

What is the difference between package and folder?

Packages are self-contained Project Files. Everything you need for your Project is saved inside the Package. Which makes it easy to share. Folders are slightly more transparent and accessible.


1 Answers

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.

like image 50
Jon Skeet Avatar answered Oct 08 '22 13:10

Jon Skeet