Reputation: 11649
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:
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:
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:
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?
Upvotes: 1
Views: 3681
Reputation: 11649
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:
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.
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.
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.
Upvotes: 1