简体   繁体   English

为什么Eclipse不总是在Eclipse Package Explorer中以相同的方式显示文件夹和软件包?

[英]Why doesn't Eclipse always display folders and packages the same way in Eclipse Package Explorer?

I can't understand how to create folders and packages in Eclipse, specifically in the Package Explorer. 我不明白如何在Eclipse中,特别是在Package Explorer中创建文件夹和软件包。 Eclipse seems to create and represent the source folders and packages inconsistently. Eclipse似乎不一致地创建和表示源文件夹和软件包。

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. 其中很多与使用标准文件夹结构(src / main / java,src / main / test等)有关,然后在其下面放置软件包。 Sometime Eclipse collapses the folders, with the packages underneath it, like this: 有时Eclipse折叠文件夹及其下面的包,如下所示:

在此处输入图片说明

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. 它不是单独的“ src / main / java”条目,而是分别嵌套到“ src”,“ main”和“ java”文件夹中。 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. 例如,它没有显示“ com.pluralsight.model”,而是具有该包的嵌套文件夹,其中“ com”,“ pluralsight”和“ model”分开。

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: 现在,我知道文件系统中的实际文件夹结构通常是单个文件夹(至少是惯例,尽管可以进行不同的设置),如文件浏览器中spring_sample文件夹结构所示:

文件资源管理器截图

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. 但是,如果我在另一个普通的Java / Maven项目中创建一个src / main / java文件夹,然后尝试在该文件夹中创建一个新包,则无法在“新建/包”对话框中选择它,并且在根文件夹(而不是src / main / java下)中创建包, 并且该包不显示为包-而是显示为单个嵌套文件夹。 It doesn't matter where I put the source files that call out that package. 放在哪里可以调用该程序包的源文件都没关系。 See here: 看这里:

Eclipse中的软件包浏览器的图像,显​​示了单独的文件夹

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? 如何在Eclipse中我想要的地方创建空软件包,并使它们显示为折叠的文件夹或如上面所示的“ spring_sample”项目中的软件包? Why do they show up as individually nested folders sometimes instead? 为什么有时将它们显示为单独嵌套的文件夹?

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. 事实证明,您需要在Eclipse中配置源文件夹,方法是将它们添加到Build Path ,然后Eclipse会折叠它们并将它们作为包显示在其中。 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 . 在Package Explorer中右键单击该文件夹(例如src/main/java ),然后选择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 . 右键单击Project,选择Properties ,然后单击Java Build Path At the top of the Java Build Path window on the right are several tabs. 右侧的Java Build Path窗口顶部是几个选项卡。 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. 注意:如果“项目属性”窗口中没有“ Java构建路径”选项,则该项目未配置为Java项目。 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. 您可以右键单击项目,然后在“配置”菜单中尝试一些操作,例如将项目转换为分面表单 ,然后在列表中选择Java复选框。 If the Project already has Java code/configuration data, you may be able to use Configure and Detect Nested Projects to automatically detect Java. 如果项目已经具有Java代码/配置数据,则可以使用“ 配置和检测嵌套项目”来自动检测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. 如果您使用的是Maven,则另一个选择是将其转换为该菜单中的Maven项目(因此它将具有pom.xml文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM