1

I can't understand how to create folders and packages in Eclipse, specifically in the Package Explorer. Eclipse seems to create and represent the source folders and packages inconsistently.

A lot of this has to do with using the standard folder structure (src/main/java, src/main/test, etc.), and then having packages underneath them. Sometime Eclipse collapses the folders, with the packages underneath it, like this:

enter image description here

While other times, the folders are nested underneath each other, with each folder separate. Rather than a single "src/main/java" entry, it is nested into "src", "main", and "java" folders separately. Similarly, sometimes it doesn't show the packages, but actually shows the individual folders. For example, instead of showing "com.pluralsight.model", it has the nested folders for that package with "com", "pluralsight", and "model" separate.

Now I get that normally the actual folder structure in the file system is normally the individual folders (at least that is the convention, though you can set things up differently), as can be seen in the spring_sample folder structure in File Explorer:

File Explorer Screenshot

But, if I create a src/main/java folder in another normal Java/Maven project, and then try to create a new package in that folder, I can't choose it in the "new/package" dialog box, and it creates the package in the root folder, rather than under src/main/java, and it doesn't show up as a package - it shows up as individual, nested folders. It doesn't matter where I put the source files that call out that package. See here:

image of package explorer in Eclipse showing separate folders

So what's going on here? How can I create empty packages in Eclipse where I want them and get them to show up as either collapsed folders or as packages like in the "spring_sample" project shown above? Why do they show up as individually nested folders sometimes instead?

8
  • Wow, that was a fast negative vote - care to explain how to improve my question or how it does not comply with the question standards? Commented Dec 23, 2018 at 17:34
  • A "package" is a folder below a "source folder" that contains a Java type (class, interface, enum, annotation, ...) Commented Dec 23, 2018 at 17:35
  • @Andreas I understand that, but why does Eclipse show these folders different ways - and inconsistently (for example, with the folder icon vs. the package icon)? Per my question, why are they sometimes shown as nested folders (specifically in the package explorer, not the project explorer) in Eclipse, and not shown that way other times? Commented Dec 23, 2018 at 17:37
  • 1
    src/main/java shows that way because the java folder is a "source folder". Your second project doesn't seem to have any source folders defined. --- Notice how a package has an icon that is a yellow square with a big plus, and how a source folder has a icon that is an open folder with a package icon in it. Also notice how src and main show as normal folders towards the end in the first project, but java is missing down there, because it is shown as a source folder up top. Commented Dec 23, 2018 at 18:09
  • 1
    Yes - that was it, finally figured that out by playing around in the context menus and Project Properties. Thanks for the help! I'm still confused on the down-votes, lol. This site is so warped sometimes. Commented Dec 23, 2018 at 18:14

1 Answer 1

1

It turns out, you need to configure your source folders in Eclipse, by adding them to the Build Path, before Eclipse will display them collapsed and display folders inside them as packages. This is available at least two ways:

  1. Right-click on the folder (such as src/main/java) in Package Explorer, and select Build Path -> Use As Source Folder. This adds that folder directly as a source folder and will immediately show it "collapsed" and turn folders underneath it into packages.

  2. Right-click on the Project, select Properties, then click on Java Build Path. At the top of the Java Build Path window on the right are several tabs. Click on the Source tab. Click the Add Folder button on the right and select the folder you will use to store source code.

enter image description here

Note: If there is no "Java Build Path" option in the Project Properties window, then this Project is not configured as a Java Project. You can right-click on the Project and try some things in the "Configure" menu, such as Convert Project to Faceted Form and then select the Java checkbox in the list. If the Project already has Java code/configuration data, you may be able to use Configure and Detect Nested Projects to automatically detect Java. Another option would be to convert it to a Maven project (so it will have a pom.xml file) in that menu, if you are using Maven.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.