6

I am trying to create a package merge under sort, but it it saying that:

Source folder is not on the Java build class path

enter image description here

So I right click on sort folder, and try to add it to the Java build class path. But only option there is exclude, so it means it should already be included to class path. enter image description here

So how can I create the package under sort?

EDIT:

added .classpath :

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
Jaanus
  • 16,161
  • 49
  • 147
  • 202

6 Answers6

7

Do it from the context menu of the "src" folder itself. The "Source folder" literally means a Source Folder as designated in the Java Build Path, not just some folder that happens to contain source. The Name field wants the actual, proper in the "this is what a .java file in it would declare", qualified package name. If you just wanted to create a new folder in there to then make some new .java files, you can just use the New Folder wizard.

EDIT: Also, keep in mind that the Package Presentation setting (set from the view's local menu--the upside down triangle) might be different between the Project and Package Explorer views. Adjust as you see fit if things look the opposite of how you'd like.

nitind
  • 19,089
  • 4
  • 34
  • 43
  • Im not following, so I can't do what I want with package right? Only folders? – Jaanus Jun 06 '12 at 17:45
  • Do it from the src folder's context menu, and for the name use the actual value you'd put in your source files. – nitind Jun 08 '12 at 05:04
  • So I right click on the src and press add package? I tried, created packaged named merge, but it justs sits next to the sort folder, and I can not move it into sort folder. – Jaanus Jun 08 '12 at 05:16
  • The actual value you'd put in your source files isn't "merge", is it? Use "sort.merge". And how are you prevented from moving it into the sort folder? – nitind Jun 08 '12 at 05:17
  • Whatever I am doing, do I need to make the sort folder as source folder? At the moment I have not added sort as source. So only source folder is "src". And now I have 2 packages named `sort.insert` and `sort.merge` sitting next to each other in src folder. – Jaanus Jun 08 '12 at 05:24
  • No. src is already a Source Folder; everything under it is already treated as source laid out in a folder<->package sense. So you have the result you wanted, now? – nitind Jun 08 '12 at 05:29
  • Confused a little bit, hehe. Well can I make them go inside each other, like a structure? So When I open up `src` , then I see `sort` and when I open up `sort`, then I see my `merge` and `insertion`. – Jaanus Jun 08 '12 at 05:34
  • Yes. As they tried to explain below, at compile time and at runtime, the compiler/VM will take the package name and use it to compute a filename, converting the segments of the package into a nested folder path. The Source Folder on the build path is one of the "roots" from where it attempts to compute this path. So the merge.sort package under a Source Folder named "src" is expected to be physically at src/merge/sort. A folder with the path src/merge/insertion is expected to contain files declaring themselves as being in the merge.insertion package. – nitind Jun 08 '12 at 05:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12287/discussion-between-jaanus-and-nitind) – Jaanus Jun 08 '12 at 05:38
5

just right click on the folder src -> build path -> use the source folder is just this

Renato Vasconcellos
  • 437
  • 1
  • 6
  • 11
2

Have you already created the merge package. Maybe you have the option of eclipse not to show empty packages on. If you have created it, just right click on the MergeSort.java file > Refactor > Move and select the new merge package under sort. This will move the file there and eclipse will show the package.

Alternatively if you haven't created the merge package yet, do the following: create a folder (not a package) Algorithms/src/sort/merge. Then find it in the eclipse tree > right click on it > Build Path > Use as Source Folder

Btw. you have only exclude on the sort package, because it is already on the build path (at least in the screenshot)

cheers

peshkira
  • 6,069
  • 1
  • 33
  • 46
1

Just create a package in your default source folder and name it sort.merge

Untitled
  • 781
  • 1
  • 6
  • 24
1

right button project-> properties->java Build path-> add a needed source to build path

enter image description here

Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
0

Java package name convention defines that a period . marks a subfolder. So lorem.ipsum.dolor.sit.amet will create a folder structure like lorem/ipsum/dolor/sit/amet. Eclipse supports this format. So for you to create "subpackages", you need to create a package in the source folder named sort.merge in your case. This will automatically create the desired folder structure.

But you're right, I never understood why Eclipse does not show the package structure in a way that reflects the underlying folder structure. Maybe someone can shed light on that design decision?

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • You should be thinking of them as Packages and Classes, which is what the "Flat" Package Presentation does. The underlying content could be files, or it could be in a jar. The point is that it can let you think about it in the terms of the language's notions rather than a computer's filesystem. – nitind Jun 06 '12 at 17:24
  • @nitind Well, its a bloody mess if you have a large package salad that is not organized in the project view. I don't work in all packages at once, but just in a small portion of it. Then I would like to collapse all other packages as much as possible. Maybe it's just me being old-fashioned, but I think I am not old enough to be old-fashioned yet... :P – brimborium Jun 07 '12 at 07:42
  • You *can* have more that one Source Folder in a project. – nitind Jun 08 '12 at 05:06
  • 1
    @brimborium When you are in Package Exlorer, press the little arrow in the right, and choose Package Presentation -> Hierarchical – Jaanus Jun 08 '12 at 05:48
  • @Jaanus Oh dear... Thanks a lot, why did I not see this all these years... Oo – brimborium Jun 08 '12 at 07:06