Adding folder in eclipse in src directory without making it package

后端 未结 5 1452
盖世英雄少女心
盖世英雄少女心 2020-12-24 02:13

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 packag

相关标签:
5条回答
  • 2020-12-24 02:29

    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.

    0 讨论(0)
  • 2020-12-24 02:31

    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.
    0 讨论(0)
  • 2020-12-24 02:32

    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.

    0 讨论(0)
  • 2020-12-24 02:34

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

    0 讨论(0)
  • 2020-12-24 02:53

    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.

    0 讨论(0)
提交回复
热议问题