开发者

How to create a folder in Eclipse?

开发者 https://www.devze.com 2022-12-24 13:03 出处:网络
I\'d like to create a folder under a package 开发者_如何学JAVAin Eclipse...The purpose of this folder is merely for organizational purposes.Meaning, I don\'t want it to be another package.Every time I

I'd like to create a folder under a package 开发者_如何学JAVAin 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:

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.

How can I just add an organizational folder in eclipse? Thanks


Actually, folders are packages in Java so your question doesn't really make any sense in a Java context.

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.

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.


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?

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).

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.

OSGi addresses this issue by introducing bundles, which can choose to not make packages visible to the outside. This gives you "project-private" packages.

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).


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号