5

I have project, with source folder "app", inside this package I have package "models". Can I create folder or any other kind of subdirectory within this package? So that eventually I would have something like

-app
   -models
       -Folder1
           -file1
           -file2
       -Folder2
           -file3
           -file4

When I try to force creating folder inside (by clicking new->other->folder), I cannot add anything to it.

cmd
  • 11,622
  • 7
  • 51
  • 61
Xyzk
  • 1,332
  • 2
  • 21
  • 36

4 Answers4

3

Creating a folder within a package simply creates a new package

e.g. The folder structure

 - app
    - models

equates to package app.models

Adding a new folder, Folder1 to this structure

e.g.

-app
  -models
    -Folder1

equates to the package app.models.Folder1

cmd
  • 11,622
  • 7
  • 51
  • 61
2

a folder in a package is just another package, so you want new->package then type in app.models.folder1

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    I can't :/ It creates it inside "app", and does not allow me to push it inside. – Xyzk Nov 14 '13 at 02:36
  • I have no idea what what you mean. if app is your src folder and models is a package then you should be able to create sub packages. – Scary Wombat Nov 14 '13 at 02:40
  • And it doesn't allow me to. If I right click on some file inside app, and select new I can't choose package. If I force it, it creates one inside "app" instead of views. I can't drag it inside views (it changes coursor to "not allowed" one) – Xyzk Nov 14 '13 at 02:44
  • If you want to create a new class in a new package then click on new->Class and type in the new package name. You can also click on new->package to create just a new package (folder) – Scary Wombat Nov 14 '13 at 02:47
  • Well, and eclipse doest not want to allow me to do that. – Xyzk Nov 14 '13 at 02:48
  • Please read my answer again and try to understand what you are doing wrong. Every Eclipse developer does what you are asking. – Scary Wombat Nov 14 '13 at 02:50
1

Packages and folders are slightly different conceptually.

You generally group your classes that deal with the same functionality in the same package. For example the core classes, the model of your app can be in com.example.myapp.core and the ui classes in com.example.myapp.ui .These packages are represented on the disk by a folder structure.

In my opinion, you should not change this package structure to add files that are not Java classes. I would suggest to add a resources folder at the top of your app tree so that your data and your classes are separated.

But if you want to just add subpackage, do not create a new folder, just create a new package such as app.models.Folder1.file1 and you will get the structure you want.

You can refer to this question to know more about the packaging conventions: Are there best practices for (Java) package organisation?

Community
  • 1
  • 1
Julien Bourdon
  • 1,713
  • 17
  • 28
  • Thank you, that helped. It simply appears Eclipse does not allow to actually put package inside package, but I could create one the way you suggested. – Xyzk Nov 14 '13 at 02:47
  • 1
    It is because folder and package are not the same thing. Packages are a concept in object programming, such as Java, that happen to be represented by a corresponding folder structure on the disk. – Julien Bourdon Nov 14 '13 at 02:49
  • https://stackoverflow.com/questions/11772969/package-presentation-in-java-ee-perspective-in-eclipse this may be the answer you are looking for – Ravinda Lakshan Nov 27 '19 at 11:44
1

The default package representation is Flat. You want to change it to Hierarchical.

enter image description here

Cameron Hudson
  • 3,190
  • 1
  • 26
  • 38
  • This is the answer you are looking for. You can create another package and if the package presentation is Hierarchical , that's why you get the mentioned scenario. – Ravinda Lakshan Nov 27 '19 at 11:41