如何在 Eclipse 中创建文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2520390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a folder in Eclipse?
提问by Polaris878
I'd like to create a folder under a package in Eclipse... The purpose of this folder is merely for organizational purposes. Meaning, I don't want it to be another package. Every time I try to add a folder under a package, it just creates a package instead. I'd like to have the following structure:
我想在 Eclipse 中的一个包下创建一个文件夹......这个文件夹的目的只是为了组织目的。意思是,我不希望它成为另一个包。每次我尝试在包下添加文件夹时,它只会创建一个包。我想有以下结构:
project/src/package1/someClass.java
project/src/package1/someFOLDER/anotherClass.java
project/src/package1/package2/anotherFOLDER/oneOtherClass.java
Is it possible to do this without adding a package? I come from a .NET/C# and C++ background... here I'd just add a folder and the reference to that class would be updated in the project.
是否可以在不添加包的情况下执行此操作?我来自 .NET/C# 和 C++ 背景......在这里我只是添加一个文件夹,并且对该类的引用将在项目中更新。
How can I just add an organizational folder in eclipse? Thanks
如何在 Eclipse 中添加组织文件夹?谢谢
采纳答案by Thilo
WTF are you supposed to do when you need multiple classes to be package private in a large project? Just look at your 25-30 class files all in the same directory?
当您需要在大型项目中将多个类打包为私有时,您应该做什么?看看你的 25-30 个类文件都在同一个目录中吗?
That is a problem with Java's package system. Every package is a directory, and sub-packages are just different packages (no special visibility rules).
这是Java的包系统的问题。每个包都是一个目录,子包只是不同的包(没有特殊的可见性规则)。
The most coarse visibility level is package-private, so that, yes, you have to lump your 25-30 files into the same package to avoid universally public visibility.
最粗略的可见性级别是包私有的,因此,是的,您必须将 25-30 个文件集中到同一个包中,以避免普遍公开可见性。
OSGi addresses this issue by introducing bundles, which can choose to not make packages visible to the outside. This gives you "project-private" packages.
OSGi 通过引入捆绑包解决了这个问题,捆绑包可以选择不让包对外部可见。这为您提供了“项目私有”包。
Update:Also, you can reduce the number of files by putting related classes into the same source file. Only public classes need to have their own source file (I do prefer to have one file per class, though, public or not).
更新:此外,您可以通过将相关类放入同一个源文件来减少文件数量。只有公共类需要有自己的源文件(我更喜欢每个类有一个文件,不管是不是公共的)。
回答by Martin
Actually, folders arepackages in Java so your question doesn't really make any sense in a Java context.
实际上,文件夹是Java中的包,因此您的问题在 Java 上下文中没有任何意义。
The term 'package' might be misleading... a package is definitely not the same as an assembly in .NET. You can think of 'package' more as a namespace, which in the Java world happens to be determined by the directory path.
术语“包”可能会产生误导……包绝对不同于 .NET 中的程序集。您可以将“包”更多地视为一个命名空间,它在 Java 世界中恰好由目录路径决定。
For non-source files, you can create a new folder outside of your source folder, it will then be treated just like any other folder and not a package.
对于非源文件,您可以在源文件夹之外创建一个新文件夹,然后将其视为任何其他文件夹而不是包。
回答by matt b
Well packages ARE folders.
那么包是文件夹。
If you want Eclipse to not build the contents of the package/folder, right-click on the project, select Properties, and under Java > Build Path edit the inclusion/exclusion filters.
如果您希望 Eclipse 不构建包/文件夹的内容,请右键单击项目,选择属性,然后在 Java > 构建路径下编辑包含/排除过滤器。
回答by Rob Heiser
If you're creating the folders to separate classes for organizational purposes, but still want them to be in the same package for access purposes, you can create multiple source folders and have them build into the same location. So, the folder hierarchy will look slightly different:
如果您正在创建文件夹以出于组织目的分隔类,但仍希望它们位于同一个包中以进行访问,您可以创建多个源文件夹并将它们构建到同一位置。因此,文件夹层次结构看起来会略有不同:
project/concern1_src/package1/someClass.java
project/concern2_src/package1/anotherClass.java
project/concern3_src/package1/package2/oneOtherClass.java
As I said, all three source folders can build into the same target directory. Or they can build into different target directories, if you like. The separate source folders will also carry over to the Package Explorer.
正如我所说,所有三个源文件夹都可以构建到同一个目标目录中。或者,如果您愿意,它们可以构建到不同的目标目录中。单独的源文件夹也将转移到包资源管理器。
回答by Bathakaraithangam
In eclipse the src folder is the main Source folder. First you just remove from the build source by right click the src folder and choose build path-> "Remove from build path". After that create the folder under src and right click the subfolder (package1) and again select buildpath->Use as Source Folder. Now you get src/package1.
在 eclipse 中,src 文件夹是主要的 Source 文件夹。首先,您只需通过右键单击 src 文件夹并选择构建路径->“从构建路径中删除”来从构建源中删除。之后在 src 下创建文件夹并右键单击子文件夹 (package1) 并再次选择 buildpath->Use as Source Folder。现在你得到了 src/package1。