2

When I use Eclipse in Java development, it seems you can never make package name and folder structure different.

For example, if you create folder structure src/com/naishe/test and create class under it, the package name will be com.naishe.test, see:

Java Package Vs Folder-Structure? what is the difference

However, i've seen a project example (maven test) where their class path is:

src/test/java/uk/co/automatictester/jwebfwk/page/objects/MainPage.java

and package definition is:

package uk.co.automatictester.jwebfwk.page.objects;

public class MainPage extends ParentPage {
 ...
}

the preceding test/java is not in the package name.

How is it done (in Eclipse)?

halfer
  • 19,824
  • 17
  • 99
  • 186
user1559625
  • 2,583
  • 5
  • 37
  • 75
  • I would very much recommend you to research the `classpath` rather than trying to explain it from the perspective of tooling that tries to hide those things from you. If you know how the Java classpath works, you don't ever have to wonder about these things ever again. That's a promise. – Gimby Nov 26 '15 at 13:08
  • @Gimby Thanks for your advice. – user1559625 Nov 26 '15 at 22:10

4 Answers4

7

The package name starts from the base of a source directory. If you make src/test/java a source directory then the given example is correct.

5

It depends on what you have set as your source directory, as an example get any folder in your directoy tree right click and put Build Path-> use as source folder

jstuartmilne
  • 4,398
  • 1
  • 20
  • 30
2

It seems to me that you have a maven project.

You have two options to have this project shown correctly in eclipse:

  1. Download http://www.eclipse.org/m2e/ maven plugin for eclipse and then you can make the project as maven project. You will automatically get the right structure in the project.
  2. Other option is to run mvn eclipse:eclipse in the console. This will add the .project and .classpath to your project. And eclipse will display them correctly.
awsome
  • 2,143
  • 2
  • 23
  • 41
0

If I understand you correctly, what you ask can be done by creating a source folder and naming it src/test/java: right click over the project, new and Source Folder.

Dani
  • 3,744
  • 4
  • 27
  • 35