Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Package and Directory in Java

Tags:

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.

like image 913
jaymeht Avatar asked Sep 29 '12 12:09

jaymeht


People also ask

What is difference between package and directory?

An interesting consequence of the package specification mechanism is that files that are part of the same package can exist in different directories. The package name is the same for each class, but the path to each file might start from a different directory in the class path. ...

Is directory and package are same in Java?

The package name relates to the directory structure in which the class resides. The integrated file system supports Java classes in a hierarchical file structure that is similar to what you find on most PC and UNIX systems.

What is a package directory Java?

A Java package is like a directory in a file system. In fact, on the disk a package is a directory. All Java source and class files of classes belonging to the same package are located in the same directory. Java packages can contain subpackages. Java packages can thus make up what is called a package structure.

What is the difference between folder and package in eclipse?

The difference is the Build Path: whatever folder is under the build path is a JAVA package. If your build path is /src/main/java then src, main, java are folders. If inside /src/main/java , your app is structured like this com/foo/bar/Main. java, then com, foo and bar are packages.


3 Answers

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.

like image 200
Hot Licks Avatar answered Oct 07 '22 17:10

Hot Licks


  • 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..
like image 38
Rohit Jain Avatar answered Oct 07 '22 17:10

Rohit Jain


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.

like image 26
om39a Avatar answered Oct 07 '22 15:10

om39a