'IntelliJ IDEA java directory paths problem

I have a problem with paths. I've got a test project like this below and I want to create a JSON reader in Main so I need a test_json path. I don't wanna use an absolute path D:..., but a path "from project" (I mean smth like main://resources/json_test/test_json.json). Is there any way to do that? enter image description here



Solution 1:[1]

If the file is in your resources folder, use getResource.

If calling from a non-static method:

this.getClass().getClassLoader().getResource("/json_test/test_json.json");

If calling from a static method:

<ClassName>.class.getClassLoader.getResource("/json_test/test_json.json");

The getResource() method will look for the path inside the src/main/resources folder, so your path should include any subdirectories you might have created inside the resources folder for the file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asif Bhuyan