Adding folder in eclipse in src directory without making it package

霸气de小男生 提交于 2019-12-18 10:37:24

问题


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


回答1:


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.



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/8025371/adding-folder-in-eclipse-in-src-directory-without-making-it-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!