How to create a folder in Eclipse? - java

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

Related

How to properly add resources to a java eclipse project [duplicate]

I know the file needs to be where the getClass().getResource(filename) can find it, but I don't know where that is.
I'm interested both in where to put the files on the filesystem itself, and how to go about using Eclipse's functionality to set up the resources.
For Eclipse, typically all you need to do is set up a folder somewhere within your source code directory. For instance, if the directory containing your source is /src then you can create a /src/resources folder to place your images/files in. Then, within your class you do a getResource("/resources/image.png") to retrieve it.
You can also place the image/file within the same folder/package as the class trying to access it if you wish (example: place the image.png in the com.mycompany package with the com.mycompany.Foo class that needs to access it and call getResource("image.png")), but I've found it's easier to keep resources like images and other files in their own special directory outside of the class folders -- they're just easier to manage that way.
In Eclipse, whenever you do a build, the files within this resource directory will be copied over into your build directory along with your compiled classes.
It's important to note that if you have "Build Automatically" turned on in Eclipse (as most people do) any resources in this directory that get changed outside of Eclipse (i.e. you edit an image using an image editing tool) that the IDE may not always detect this change. Usually doing a refresh on the project folder will ensure that the file gets updated in the build in these situations.
You can either put them in the src folder alongside your classes, or you can create a new source folder for the purpose (usually called resources), although you'll locate them identically from code.
Then you get at them using getResource("/com/x/y/foo.png").

Can't make a folder in Eclipse

All I want is to make a folder at the "root" of my eclipse stuff so I can store projects from different classes together. However, it won't let me do that, the "Finish" button is greyed out until I select a parent folder - I must choose from one of the many projects I want IN this new folder. What do I do?
It kind of sounds like you'd be best served by a single project that contains all of the source files from your classes. The workspace is the root directory and any subdirectories are expected to be individual projects.
I agree with what everyone has been saying, create a project for the entire class as 1 project. Then you can create as many classes as needed with main() and only export that single class (or dependent classes as needed) to turn in. In addition, you could package each individual project assignment in a separate package. As stated above, leave your root (workspace) where it is.
Instead of creating multiple projects you may use Java packages option. Create a new package and put all your additional classes over there. Please note that all Java files should be under src/ directory of the eclipse project.

Regard creating a Java project in Eclipse

In the “create a Java project” wizard.
For the “project layout”, there are two choices:
1) use project folder as root for sources and class files.
2) Create separate folders for source and class files
Which one should I choose?
For the “Working set”
Whether I need to check the “Add project to working set”? What does it mean?
I always choose Create separate folders for source and class files, it's just separate your src files and your output files
The one you choose is up to you. It doesn't matter one way or another, at least as far as your tools are concerned.
The first option means that all files will be in the root directory of the project (typically PATH_TO_WORKSPACE/projectName). Your .java and .class files will be here if you choose that option. The second option will create PATH_TO_WORKSPACE/projectName/bin and PATH_TO_WORKSPACE/projectName/src. Your source files will be in /src and your compiled files will be put into /bin.
My personal preference is to not use the project folder as the root for sources and class files and to create separate folders for source and class files. However, it's all up to you.
In my opinion, choose different folders for sources and binaries. It will make source control and versioning easier.
Working sets only make sense when you are using more than one project for one workspace. I would guess that you won't need working sets until you are more experienced with Eclipse.
it is only a metter of user convinience. eclipse is able to handle both ways.
working set is a way to handle eclipse workspace when you have many projects. to get started, you don't need that.

Java -- how do I access (import) the class files in a folder that is in the same directory as the src folder?

In eclipse or netbeans or emacs pico or microsoft word or notepad or or or whatever. Thanks.
In eclipse, I'm trying to share a package in one project w/ another project. I do the whole buildpath->link source -> add source ... and it creates a linked directory but not as package within the src/ dir, instead as a separate dir sibling to the src/. so I'm left stranded, wondering, waiting for a solution.
If you mean classes without a package, you can only refer from them outward, you can't refer from a class that is in a package to a class with no package declaration (A very good thing).
Even if you wanted to reference a class in the default package it would be in your "src" folder, not in the same directory as your src folder, so perhaps you mean something different?
Generally your "src" directory would contain a "com" directory--the top of the package tree. So if you had a class in the default package you would place it at the same level as "com", but as I said you can't refer to it from other code--you can only execute the unpackaged class from the command line.
If you are saying that you want all your classes in the same directory or something--then I recommend packaging them all together as a .jar file.
Normally you would add the project (with classes you want to share) to the Build Path (right-click>Build Path>Configure Build Path) of the consuming project. That allows you to compile and run within eclipse.
If you want to create a link from one project to the place where the other project code lives, you have to create your linked folder in such a way that 1) it's already in the /src/ folder or 2) can be turned into a source folder and can see the full package structure necessary to compile your classes. But that is not how most java projects consume the output of another java project.

Creating a separate Folder in the same package... [ECLIPSE]

Is it possible in Eclipse to create a separate Folder in a package? When select "new --> folder", my eclipse version always creates a package. But i want the new folder to be in the old package.
How can this be achieved?
Thanks in advance...
Patrick
What you might want to do, if you aren't putting code in that folder, is remove the folder from the build path. Right-click on the offending folder in eclipse and choose 'exclude from build path.'
Or, you may want a different effect:
Right click on your project and choose add new source folder.
Then in that folder, add the correct package folder structure.
You can have files in the same package located in different folders.
You need to exclude the folder from the source path
In a source folder, all 'new' folders will be considered a new package. It's part of the Java spec that makes all folders from a root to be part of the package structure.
As others have said, this is standard java behaviour and shouldn't really be worked around.
You are probably better off creating another folder outside of the source folder.
However there is a way to do it in Eclipse, be warned that this might come back to haunt you if you later try to build outside Eclipse as other tools won't honour these settings.
You can exclude a folder from the source path by opening the project properties.
Selecting Java Build Path->Source
Select the appropriate source folder and select the Excluded: child item
Hit the Edit... button.
Select the Add button next to the Exclusion patterns: pane
Add your folder and Finish/OK back to the editor.
The matching resources will now be excluded from compilation within Eclipse. It will still appear as a package in the editor though.
You want to create a new child package (bar) of the existing package. (com.foo)
Select new package and name it "com.foo.bar". Eclipse will correctly place it under com.foo.
FYI: Folders under any "source" folder are shown as packages. There is no physical difference except that they appear on the build path. As shown elsewhere you can exclude them but it's easier to just not put them under /src/... to begin with.

Categories

Resources