Home > Net >  IntelliJ IDEA java directory paths problem
IntelliJ IDEA java directory paths problem

Time:04-07

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

CodePudding user response:

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.

  • Related