2

Some of our developers prefer Eclipse and some prefer Netbeans. We want the same file hierarchy for both so that it's kept under one directory in subversion.

The directory structure is:

src
| +main
| | +java
| |   +com
| |     +mycompany
| |       +utils
| |         +Bar.java
| |         +Foo.java
| +test
| | +java
| |   +com
| |     +mycompany
| |       +utils
| |         +BarTest.java
| |         +FooTest.java
bin
| +com
| | +mycompany
| |   +utils
| |     +Bar.class
| |     +Foo.class

This not only works fine in Netbeans, but it appears to be a standard. (http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html) The problem I'm having is that in Eclipse I can see all this hierarchy, but it complains that the file Bar.java (above), which has a package declaration: "package com.mycompany.utils;", should be declared as "package main.java.com.mycompany.utils;", which is not what I want. How can I configure Eclipse to recognize this hierarchy?

Thanks,
Chelmite

Chelmite
  • 360
  • 3
  • 18

3 Answers3

0

Eclipse is not recognizing the proper class hierarchy because the definition of source folders is in its metadata files, which you have probably not checked into SVN. Since you seem to be using Maven I would recommend you have your developers install m2e, which natively recognizes and handles Maven projects, no extra setup required.

Indigo and Helios come with m2e installed afaik.

If you want to manually fix up those source trees, then just right click on the project and define two source folders

src/main/java
src/test/java
Perception
  • 79,279
  • 19
  • 185
  • 195
  • If I open the Properties for the project, go to Java Build Path, then the Source tab, it shows my src folder. If I click "Edit", if I change "src" to "src/main" or "src/main/java", it complains that the folder already exists! – Chelmite Mar 29 '12 at 20:29
  • This issue with already existing folder is resolved here: http://stackoverflow.com/questions/11976223/how-to-deal-with-missing-src-test-java-source-folder-in-android-maven-project –  Mar 23 '13 at 22:44
0

You need to edit your classpathentry. Try editing the .classpath file for your eclipse project and changing it to something like the following:

<classpathentry kind="src" output="bin" path="src/main/java"/>
Mike Bockus
  • 2,079
  • 17
  • 23
  • I tried: But it still complains that com.mycompany.utils should be src.main.com.mycompany.utils – Chelmite Mar 29 '12 at 20:44
  • Do you have any other source folders defined? My .classpath looks like this: ` ` The project looks like [this](http://i.imgur.com/ja7S8.png) in the package explorer view. – Mike Bockus Mar 30 '12 at 02:44
0

The mismatch is that your Eclipse project is configured with src as the root of the Java source path, when in fact you have two separate source trees both under that folder. To configure it correctly:

  1. Open the Project Properties (via right-click)
  2. Select Java Build Path on the left, then the Source tab on the right side
  3. Remove src as a source folder
  4. Add src/main/java and src/test/java as source folders

That tells Eclipse that the project has two source folders and it will compile them together into the bin folder.

Note: it is strongly recommended to check in the .project and .classpath files and the .settings folder into svn, so that anyone who checks out the project into Eclipse will automatically get this configuration.

If wanted, you could configure separate build output locations for each of the source folders (instead of both going into bin).

E-Riz
  • 31,431
  • 9
  • 97
  • 134