Java 8 Solution
Path source = Paths.get(this.getClass().getResource("/").getPath());
Path newFolder = Paths.get(source.toAbsolutePath() + "/newFolder/");
Files.createDirectories(newFolder);
This will surely create new folder in resource folder. but you will find new folder in your target runtime.
which will be ProjectName/target/test-classes/newFolder. if you are running this code in test case. Other wise it would be in target/classes
Don't try to find new folder in your src/resources.
it will be surely in target/test-classes or target/classes.
Java 8 Solution
Path source = Paths.get(this.getClass().getResource("/").getPath());
Path newFolder = Paths.get(source.toAbsolutePath() + "/newFolder/");
Files.createDirectories(newFolder);
This will surely create new folder in resource folder. but you will find new folder in your target runtime.
which will be ProjectName/target/test-classes/newFolder. if you are running this code in test case. Other wise it would be in target/classes
Don't try to find new folder in your src/resources.
it will be surely in target/test-classes or target/classes.
As other people have mentioned, resources are obtained through a ClassLoader. What the two current responses have failed to stress, however, is these points:
- ClassLoaders are meant to abstract the process of obtaining classes and other resources. A resource does not have to be a file in a filesystem; it can be a remote URL, or anything at all that you or somebody else might implement by extending
java.lang.ClassLoader. - ClassLoaders exist in a child/parent delegation chain. The normal behavior for a ClassLoader is to first attempt to obtain the resource from the parent, and only then search its own resources—but some classloaders do the opposite order (e.g., in servlet containers). In any case, you'd need to identify which classloader's place for getting stuff you'd want to put stuff into, and even then another classloader above or below it might "steal" your client code's resource requests.
- As Lionel Port points out, even a single ClassLoader may have multiple locations from which it loads stuff.
- ClassLoaders are used to, well, load classes. If your program can write files to a location where classes are loaded from, this can easily become a security risk, because it might be possible for a user to inject code into your running application.
Short version: don't do it. Write a more abstract interface for the concept of "repository of resource-like stuff that I can get stuff from," and subinterface for "repository of resource-like stuff that I can get stuff from, but also add stuff from." Implement the latter in a way that both uses ClassLoader.getContextClassLoader().getResource() (to search the classpath) and, if that fails, uses some other mechanism to get stuff that the program may have added from some location.
To add a resource folder in eclipse:
Build Path
Configure Build Path
(or Properties -> Java Build Path)
Source Tab

Click on Add Folder

Create new Folder The same problem I also encountered while I was learning the spring boot wanted to add logback.xml in resources.
I am using sts for spring boot. As of my understanding, there are two things your file system folder structure and another sts folder structure.
First I created a folder in your file system at src/main/ and then add this folder into build path. *
For creating a folder follow below image 1,2 and 3
*



Now at the final step, add newly added resource folder into the build path. by clicking properties or directly build path button.
Click on source tab and then click on add folder and browse the resources folder and add that's all. See below image for more description and help.

When at the "Add resource folder",
Build Path -> Configure Build Path -> Source (Tab) -> Add Folder -> Create new Folder

add "my-resource.txt" file inside the new folder. Then in your code:
InputStream res =
Main.class.getResourceAsStream("/my-resource.txt");
BufferedReader reader =
new BufferedReader(new InputStreamReader(res));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
To answer your question posted in the title of this topic...
Step 1--> Right Click on Java Project, Select the option "Properties"
Step 2--> Select "Java Build Path" from the left side menu, make sure you are on "Source" tab, click "Add Folder"

Step 3--> Click the option "Create New Folder..." available at the bottom of the window
Step 4--> Enter the name of the new folder as "resources" and then click "Finish"

Step 5--> Now you'll be able to see the newly created folder "resources" under your java project, Click "Ok", again Click "Ok"

Final Step --> Now you should be able to see the new folder "resources" under your java project

You can use java.utils to create a folder inside the resource directory
new File("/path/directory").mkdirs();
"directory" is the name for the folder you want to create
you can get the root of the classpath from new ClassPathResource(".").getFile().getPath() and after that you can append the folder name that you want. after Make the File object by using that full path. and then you can create a folder as you want in the class path.
File txtFile = new File(new ClassPathResource(".").getFile().getPath()+"/tmp");
if (!txtFile.exists()) {
txtFile.mkdirs();
}
Caveat: I don't use Eclipse, I've never used Eclipse, my knowledge of Eclipse amounts to reading other posts and answering questions here
Under your project, create a directory called resources. In here place all you "resources" you want to access from within your application. These will be bundled with your application as embedded resources.
From within your application use either
this.getClass().getResource("/ah.wav")
or
this.getClass().getResource("/resources/ah.wav")
Assuming that you placed the files at the top level of the resources path. If they're in a sub-directory, you'll need to provide the full path...
Clean, build and test
I'd create a resources folder inside src something like src/main/resources. You would like to take a look to Maven that is project management and comprehension tool that help a lot in project organization.
new File("/path/directory").mkdirs();
Here "directory" is the name of the directory you want to create/exist.
After ~7 year, I will update it to better approach which is suggested by Bozho.
File theDir = new File("/path/directory");
if (!theDir.exists()){
theDir.mkdirs();
}