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.

Answer from Rohit Kumar on Stack Overflow
Top answer
1 of 6
15

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.

2 of 6
11

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.

🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000655999-Creating-or-jumping-to-a-resource-folder-for-a-given-class
Creating or jumping to a resource folder for a given class – IDEs Support (IntelliJ Platform) | JetBrains
If that folder doesn't exist yet I copy the relative path of the java file, remove everything before "src", navigate to the resources folder, create a new folder and paste the path.
🌐
Java by examples
javaquery.com › 2015 › 10 › how-to-create-resource-folder-in.html
How to create resource folder in Netbeans? - Java by examples
Note: On successful compilation of project all resource file will be stored in bin folder of project with same structure of resource folder. Step 1: Right click on Project and go to Properties. Step 2: Go to Sources and click on Add Folder...
🌐
YouTube
youtube.com › watch
How to create a Resource folder in Java with IntelliJ and how to access it - YouTube
I will be showcasing how to create a resource folder in Intellij, and I will create a method that readers a file form the resource folder and print the input...
Published   March 5, 2021
🌐
Chief Delphi
chiefdelphi.com › technical › java
How to create a "resources" directory from gradle? - Java - Chief Delphi
January 17, 2020 - I want to write some data using gradle into the src/main/resources directory to be included int he deployed jar file. I have it working if the resources directory is already there, but is there a way to get gradle to create the directory if it isn’t already there?
🌐
Bloomu
javabook.bloomu.edu › addresources.html
How to Add Resources to an IntelliJ Project
The Practice and Philosophy of Object-Oriented Programming in Java ... Your application can load resources such as audio and image files at runtime. For this you will need to create a resources folder in the project's root directory.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java io › create a directory in java
Create a Directory in Java | Baeldung
January 8, 2026 - In this article, we’ve seen two methods allowing us to create directories in Java. The first one, mkdir(), targets the creation of a single directory, provided its parents already exist.
🌐
Makble
makble.com › what-is-the-srcmainresources-folder-for-in-java-project
What is the src/main/resources folder for in Java project - Java - Makble
Every Java project contains a folder named resources, different type of projects have different paths, in a standard Maven project structure, the path is src/main/resources, Gradle projects inherit that layout. The path is the default value defined in Super POM.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-createdirectories-method-explained-cb824678b0b7
Java’s Files.createDirectories() Method Explained | Medium
January 26, 2025 - The createDirectories() method can be used in various scenarios where directory creation is needed, from simple use cases to more complex setups. Below are some examples to help you understand how this method works and how it can be applied.
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEA-129460 › handling-of-java-packages-and-folders-in-resources-type-source-f
handling of "java packages" and folders in "resources"
June 9, 2023 - {{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Stack Overflow
stackoverflow.com › questions › 31297503 › creating-a-new-folder-in-resources-folder-and-read-write-text-files-there-maven
java - Creating a new folder in resources folder and read-write text files there (Maven Project) - Stack Overflow
May 22, 2017 - Resources below src/main/resources/ end up in the JAR file which Maven creates - Java wants it that way. They aren't visible anymore as files when you run the JAR. That's why you can't create a path to them. To access the resources, you need to ask the ClassLoader for a URL (.getResource("...relative/path...")) or an InputStream (getResourceAsStream()). If you want to save config options, you should use the Preferences API or maybe create a Properties file in the home folder of the user or in a folder defined by a system property.
🌐
ZetCode
zetcode.com › java › createfile
Java create file - learn how to create a file in Java
The File's createNewFile method creates a new, empty file named by the pathname if a file with this name does not yet exist. ... package com.zetcode; import java.io.File; import java.io.IOException; public class JavaCreateFileEx { public static void main(String[] args) throws IOException { File file = new File("src/main/resources/myfile.txt"); if (file.createNewFile()) { System.out.println("File has been created."); } else { System.out.println("File already exists."); } } }
🌐
JetBrains
jetbrains.com › help › idea › content-roots.html
Content roots | IntelliJ IDEA Documentation
April 17, 2026 - Create a content root for your project and configure a folder structure for your production code, your tests, and resource files.
🌐
Coderanch
coderanch.com › t › 757738 › java › resources-folder
How to get resources folder (Beginning Java forum at Coderanch)
Let's assume that your project folder looks like this: In this layout, src/ and res/ are so called "source folders" that contain Java source files and resource files respectively. bin/ contains the compiled class files and lib/ contains libraries that the application depends on.
🌐
Mccue
javabook.mccue.dev › files › creating_a_folder
Create a Folder - Modern Java
import java.nio.file.Files; import java.nio.file.Path; class Main { void main() throws IOExeption { Path folderPath = Path.of("example1"); Files.createDirectory(folderPath); } } This, like the other methods in Files, might throw an IOException.
🌐
JetBrains
jetbrains.com › help › idea › add-items-to-project.html
Add items to your project | IntelliJ IDEA Documentation
March 16, 2026 - If you want to create several nested directories, specify their names separated with slashes, for example: folder/resources. Packages in Java are used for grouping classes that belong to the same category or provide similar functionality for ...