jaymeht
jaymeht

Reputation: 688

Difference between Package and Directory in Java

In a Java Project, does keeping all the .java files in the same folder mean they are in the same package?

What is the difference in making a Package for our project compared to keeping all the project files in one folder?

This thread doesn't really address my question.

Upvotes: 27

Views: 29254

Answers (3)

Hot Licks
Hot Licks

Reputation: 47729

There is a relationship between package and directory, but it's one that you must maintain. If you have a class that's in "mypackage1.mypackage2", that means that the java command is going to expect to find it in a directory structure named "mypackage1\mypackage2" (assuming "backwards" Windows notation), with that directory structure further embedded in a directory (let's call it "myjava") whose name is in the classpath (or else is directly in the "current directory").

So your Java class (which internally says package mypackage1.mypackage2;) is in, say, "\Users\myName\myjava\mypackage1\mypackage2\", and you place "\Users\myName\myjava" in the class path, or else you have your current directory set to "\Users\myName\myjava".

If you mix this up, either the class will not be found at all, or you will get an error something like the ever-nebulous "NoClassDefFoundError".

As to why one would use packages (and directories), the reason has to do with "name space" and "separation of concerns" (look those up). Java would be much harder to keep straight if there were no packages and all the "java.lang", "java.io", "sun.misc", et al classes were together. First off, one would have to use name "prefixes" to keep them straight and avoiding name conflicts. And much of the logical grouping would be lost.

With your own projects you don't need to use packages for simple little programs you write for yourself, but if you write something you might give to someone else it's polite to use a package such as "myname.myproject" (substituting your name and project of course), so the person you give it to can combine it with others without name conflicts.

In large applications you'll find using further levels of separation helps you keep the functions straight, so you know where everything is. It also discourages you from "crossing the boundary" between different functional areas, so you don't get unrelated logic entertwined.

Eclipse (if you use that) kind of muddles the issue a bit because it "wants" to provide directory and package names and will sometimes (but not always) keep them in sync.

Upvotes: 18

om39a
om39a

Reputation: 1406

Understanding the class loader subsystem is will answer your query.

With reference to Inside JVM by Bill Venners

Given a fully qualified type name, the primordial class loader must in some way attempt to locate a file with the type's simple name plus ".class". Hence, JVM searches a user-defined directory path stored in an environment variable named CLASSPATH. The primordial loader looks in each directory, in the order the directories appear in the CLASSPATH, until it finds a file with the appropriate name: the type's simple name plus ".class". Unless the type is part of the unnamed package, the primordial loader expects the file to be in a subdirectory of one the directories in the CLASSPATH. The path name of the subdirectory is built from the package name of the type. For example, if the primordial class loader is searching for class java.lang.Object, it will look for Object.class in the java\lang subdirectory of each CLASSPATH directory.

Upvotes: 9

Rohit Jain
Rohit Jain

Reputation: 213223

  • Packages provide logical namespace to your classes..

  • And these packages are stored in the form of directory levels (they are converted to nested directories) to provide physical grouping (namespace) to your classes..

Also, note that the physical namespace has to be in accordance with the logical namespace.. You can't have your class with package com.demo, under directory structure : - \com\demo\temp\, it has to be under \com\demo\, and this directory is added to the classpath so that your classes is visible to JVM when it runs your code..

Suppose you have following directory structure: -

A
|
+-- Sample.java(contains Demo class under package B)
|
+-- Outer.java(contains Demo class - no package)
|
+--B
|    |
|   +-- Demo.class
|
+--C
|    |
|   +-- Abc.class
|
+-- Demo.class

Suppose, your class Abc.class and Demo.class (under directory A), isn't define under any package, whereas your class Demo.class (under directory B) is defined under package B. So, you need to have two directories in your classpath: - \A(for two classes: - Demo.class and B.Demo.class) and \A\C(for class Abc.class)..

  • Classes under different package can use same name. That is why there won't be any conflict between the two Demo.class defined above.. Because they are in different packages. That is the whole point of dividing them into namespaces.. This is beneficial, because you will not run out of unique names for your classes..

Upvotes: 11

Related Questions