85

I would like to know What are the difference between folder-structure and package used in Eclipse IDE for Java EE development.

When do we use which one and why?.

Whats should be the practice

  • create a folder structure like src/com/utils and then create a class inside it
  • create a package like src.com.util and then create a class inside it

which option would be better and easy to deploy if i have to write a ant script later for deployment ?

if i go for the folder-structure will the deployment is as easy as copying files from development to deployment target ?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Mrinmoy
  • 1,611
  • 3
  • 18
  • 20

6 Answers6

62

If you configured stuffs correctly. Adding a folder inside src, is same as adding a package from File > New Package.

So, it's up to you, whatever feels comfortable to you -- add a folder or create a package. Also, when you put stuffs under src the package name starts from subfolder. So, src/com/naishe/test will be package com.naishe.test.

Nishant
  • 54,584
  • 13
  • 112
  • 127
18

Basically there is no difference, both are the same.

In both the cases, the folder structure will be src/com/utils.

and in both the cases, you will need to mention

package com.utils;

as first line in the class

Since it doesn't have any difference practically, it won't make any difference to ant script.

ljk
  • 488
  • 1
  • 5
  • 11
dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • "A package in Java is used to group related classes. Think of it as a folder in a file directory" sigh... then why the need to introduce yet another concept... – frankelot Nov 05 '22 at 05:53
16

"Packaging helps us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways."

check out http://www.jarticles.com/package/package_eng.html for more information on packages

user2729857
  • 161
  • 1
  • 2
  • Unfortunately this doesn't justify why I need to create `com/mycompany/project/package/name/here`-subdirectories in my filesystem. Isn't there an option to coalesce the `com/mycompany/project/package/name` directories to save typing and avoid hitting `MAX_PATH`? – Dai Dec 30 '17 at 03:02
2

create a package like 'src.com.util'

That sounds like a mistake. The package name should be 'com.util', and 'src' is the name of the source folder.

Other than that, I fail to see what the difference is between your two choices. The result is the same, right? Just different steps in the GUI to arrive at it. The wizard to create a new package in Eclipse is just a wrapper around creating the appropriate folder hierarchy within a source folder.

You don't need to create empty packages at all, you can directly create classes (the package will be created automatically if it does not already exist).

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

A package is automatically "source folder" where folder is just a normal folder. When you compile an Eclipse project, all files in source folders are compiled but not in regular folders (unless those regular folders a)

teamaster
  • 403
  • 2
  • 6
  • 16
0

folder structure or to be specific source folder in eclipse is meant just for eclipse but package is universal irrespective of any editor..

Jack
  • 29
  • 6