18

I have recently started coding on Eclipse for Minecraft Bukkit plugins, and I have been going fine with it. Recently, I've been trying to make a child package into a package, but if I try New > Package, then it makes a package separate to my main package. I am a beginner, so please make instructions simple. Is there anything else I can try?

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Permanently
  • 344
  • 2
  • 3
  • 13
  • 3
    Guess: You are viewing the package view with flat presentation. Solution: click the down arrow, and select package presentation -> hierarchical – Absurd-Mind Aug 19 '14 at 08:26
  • I think it's called "hierarchic". – EpicPandaForce Aug 19 '14 at 08:27
  • 1
    @Zhuinden yes, it's hierarchical. Checked it out in 'Team Synchronizing' there it is tree m( – Absurd-Mind Aug 19 '14 at 08:28
  • 5
    Since you are a beginner, allow me this kind advice: There are no child packages in Java. Of course there is a hierarchical way of naming packages, but the packages themselves are NEVER in any kind of hierarchy. This is important when thinking about visibility rules. That is also why Eclipse defaults to a "flat" representation. – SirRichie Aug 19 '14 at 08:29
  • Well technically yes, it's more-so a "nested package" than a package, but even so, the packages are completely independent. Which is why you need to import the other package using Ctrl+Shift+O no matter what. – EpicPandaForce Aug 19 '14 at 08:31
  • I'm disappointed in my 13-year-old self. I've learnt a lot since asking this question. – Permanently Jan 11 '20 at 23:55

5 Answers5

56

Normally, if you separate each package component with dots (.), it should create intermediate entries.

Try create a new package with the following test case:

  • com.foobar.example
  • com.foobar.test
  • com.foobar.example.a

And Eclipse should do the tricks.

If however, you are stuck with finding those empty packages, perhaps you should simply play with the Package Explorer options: the Java filters is especially useful, and there might be an option enabled by default which would filter/hide empty package.

You also have Package presentations.

Since an image is better than long lines of text:

Eclipse

  1. Click the arrow to open the presentations options (see the Package Explorer on the left)
  2. The Filters options does have filters which might hide empty parent packages.
  3. The hierarchical package presentation behave like the Explorer left pane.
NoDataFound
  • 11,381
  • 33
  • 59
8

When creating a new package you can create a hierarchy by simply separating your parent folder with child folder with a dot '.'. So if you want to create a sub package of foo.bar you have to create a package with name foo.bar.myfoo. Eclipse will do the rest.

If you instead want to visualize the folder type hierachy as here:

enter image description here

you can click in the little arrow top left of the project explorer view menù and go to Package Presentation > Hierarchical.

Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16
4

A folder-like hierachy is implicitly created by the package names in Java. Lets say you have a package com.company and you want a sub-package in that package. Then you have to create a package with the name com.company.product. It is not done by creating a package with the name product while com.company is selected!

Kai
  • 38,985
  • 14
  • 88
  • 103
2

For eclipse Version: 2019-03 (4.11.0) Build id: 20190314-1200

I found it easier to create the desired package path along with the new class I wanted to exist at the new package path.

From within the Project Explorer view, right click on the corresponding project to create a New Class.

enter image description here

Then on the class creation popup I fill in the corresponding package field as I please to build out the new path as desired, along with the new class to sit in that new path.

enter image description here

user1821961
  • 588
  • 1
  • 10
  • 18
0

For Eclipse Version 2020-09: To create sub-package we can directly go inside the eclipse-workspace and in the package where we want to create a sub-package and inside that we can create a new folder and then click on the three dots icon on the right side of the Package Explorer and select the Hierarchical option inside the package presentation option. We can then refresh the project and the new sub-packages will be reflected.Below is the image for reference Subpackage