28

While creating one folder in src directory in one project in eclipse, it makes that folder a package. Is there any way to avoid this folder from automatically being a package? e.g., I add main folder in src directory. I don't want it to become a package. How can I do this?

Suppose I add folders in this manner: src/main/org/apache. I don't want main.org.apache to be a package, instead, I want the packaging to start from org. (i.e., org.apache).

my code smells
  • 461
  • 5
  • 14
Tarun Kumar
  • 5,150
  • 6
  • 26
  • 30

5 Answers5

35

Eclipse forces you distinguish between source directories and ordinary folders. Any subdirectories in a source folder will be considered a package.

In your case, you can create an ordinary folder outside of src/ to prevent the subdirectories from being interpreted as packages.

Alternatively, you can modify the project properties to have src/ be considered an ordinary directory, and put a source directory within it.

You can manage which directories in a project are considered source directories by:

  1. Right-clicking your project, then click Properties.
  2. In the left pane, click Java Build Path. In the right pane, select the Source tab.
  3. Here you can add/edit/remove source folders.
Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
  • But if we do it this way and the files are no longer recognized as "src files", then how do we compile and run/debug the project (Ctrl F11)? – Pacerier Aug 15 '14 at 21:15
  • You're right, all of your code still needs to go into `src` folders or it won't be compiled. – Mansoor Siddiqui Aug 17 '14 at 12:03
  • 1
    Is there any way to config Eclipse such that we can put classes in a package into more than one folder? – Pacerier Aug 17 '14 at 15:29
  • If I understand you correctly, you want to have multiple source directories -- is that right? If so, you can turn an ordinary folder into a source folder by right-clicking the folder, click "Build Path", then click "Use as Source Folder". I hope that helps! – Mansoor Siddiqui Aug 17 '14 at 23:35
  • Cool it works if there are non-clashing classes =) But do you have any idea how we can make it work for clashing classes as well? Which means two different files which contributes to the contents of a single class, something like a C# `partial` http://msdn.microsoft.com/en-us/library/wa80x488.aspx – Pacerier Aug 21 '14 at 12:14
  • If it's outside the source folder, right-click the project -> Build Path -> Remove From Build Path. – person27 Aug 18 '17 at 18:00
12

You need to go to Project -> Properties -> Java Build Path -> Source (tab).

Remove src from "Source Folders on Build Path"

Then add src/main as a source folder. If you already have org under main, then your packages should start with org as desired.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
5

I added the "Excluded" pattern with value ** to 'Java Build Path -> Source - > src/main/resources' and my package became a simple folder in Eclipse.

Pasha190
  • 94
  • 1
  • 3
1

When you run a Java project in eclipse, the current working directory is the root folder of the project. So why not just create your resource folder there?

ProjectDirectory
../bin
../src
../resourceDirectory

Then, in your project, you can fetch resourceDirectory with

public File getDirectory(String dir) {
   File cwdir = new File(".").getParentFile(); // remove the "." in path
   for (File f : cwdir.listFiles()) {
      if (f.isDirectory() && f.getName().equals(dir)) {
          return f;
      }  
   }
   return null;
}

and

File resourceDir = getDirectory("resourceDirectory");
if (null == resourceDir) {
    System.err.println("Resource directory not found");
    System.exit(-1);
}

Note : you might perhaps want to create an utility class for the getDirectory() method, or something similar. This is just an example.

Bottom line is that the application will most likely be launched where it should be and you might just use a startup file (.bat, .sh, etc.) to do that anyway. There is no need of putting resource directories inside your source or binary folder; keep them separated.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

Any folder in src directory becomes a package. If you wish to have a main folder then create a source folder of name main and then create the required package in main folder.

dharam
  • 7,882
  • 15
  • 65
  • 93