Debugging getResource*
How do you debug getResource-style methods that are failing, returning null?
I am sure the file it's looking for is there, but it's returning NULL. How do I know what it is looking for to try to spot any mismatch?
Asked by: Rafael595 | Posted: 28-01-2022
Answer 1
Since getResource()
searches the classpath (as others have mentioned), it might be helpful to dump the actual classpath being searched before your problemsome getResource()
call:
log.debug("classpath is: " + System.getProperty("java.class.path"));
//the line that is returning null
... = Thread.currentThread().getContextClassLoader().getResource("foobar");
What is probably happening is that the files/directories you think are on the classpath are actually not (perhaps an invalid path is being set somewhere along the way).
Answered by: Alfred996 | Posted: 01-03-2022Answer 2
You could use the Eclipse-debug-mode and set a breakpoint on the method that fails. From there you can go step by step down in the call tree until you see what fails.
Most common is that the file isn't there because it wasn't copied there or isn't in the classpath etc...
Answered by: Rafael780 | Posted: 01-03-2022Answer 3
The getResource call is looking for a file relative to the class file.
My first guess would be that when you have compiled you have forgotten to put the resource files into the compile folder. That's what I've been running on often.
Answered by: Alberta310 | Posted: 01-03-2022Answer 4
I've usually experience this whenever the ClassLoader changes.
Depending on the exact context that an app is running ClassLoaders have different rules about when a resource file exists. For example in netbeans getResource is case insensitive, but in Sun's JRE it is.
Although not directly answering your question, I thought you should know this (if you didn't already).
Answered by: Audrey530 | Posted: 01-03-2022Answer 5
I think you should tell Eclipse or your favourite IDE where the JDK (JRE) source files can be found. Then you can step in the methods of the Java Runtime classes too.
Answered by: Lenny202 | Posted: 01-03-2022Similar questions
Still can't find your answer? Check out these amazing Java communities for help...
Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)