Skip to content
Advertisement

JSON file not copied to target folder

In my java project, I have a .json file which I have placed in resource folder. When I am trying to read this .json file in my main class using the getResourceAsStream() method, I am getting a FileNotFoundException.

ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);

I checked that under folder target > classes this .json file is not present. I am not sure why this .json file is not being copied to the target folder. Need help to understand the reason. It is a Maven project and I am using IntelliJ to build and run the project.

Advertisement

Answer

JSON files are not listed as default formats that IntelliJ will consider a resource. Did you add the file format to your deployment configuration? I will quote from the IntelliJ documentation:

When building an application, IntelliJ IDEA copies all resources into the output directory, preserving the directory structure of the resources relative to the source path. The following file types are recognized as resources by default:

  • .dtd
  • .jpeg
  • .properties
  • .gif
  • .jpg
  • .tld
  • .html
  • .png
  • .xml

The pattern of recognized resource files can be configured as a regular expression in the Compiler dialog (Settings/Preferences Ctrl+Alt+S | Build, Execution, Deployment | Compiler). Using the Resource Patterns field, you can add your own file extensions and create custom list of resources.

https://www.jetbrains.com/help/idea/resource-files.html

Add a regular expression for the JSON file format and you should be fine.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement