0

Intellij is unable to find a file on the classpath with the following java statement:

 URL fileUrl = TestClass.class.getResource("/data.xlsx");

The variable fileUrl is null;

enter image description here

user2023141
  • 895
  • 5
  • 17
  • 36
  • I guess you must add before your package name **catalogs** like this: TestClass.class.getResource("/catalogs/data.xlsx"); I'm not sure if it works due to the structure of the project – Edgar Magallon Feb 02 '21 at 14:06
  • As per the snapshot data.xlsx seems to be inside the catalogs folder.. please verify and see how you can access the resources folder and its sub folders – Nikunj Kakadiya Feb 02 '21 at 14:07
  • I changed the statement as follow : TestClass.class.getResource("/catalogs/data.xlsx"); And now the URL is set. Thanks Edgar. – user2023141 Feb 02 '21 at 14:36
  • Check this answer for the ways to load resource from classpath: https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java?rq=1 – Andrey Feb 02 '21 at 15:15

1 Answers1

0

From the comments above. I changed the statement as follow :

TestClass.class.getResource("/catalogs/data.xlsx");

Now the URL is set.

user2023141
  • 895
  • 5
  • 17
  • 36