I know there have been multiple posts on the subject but my issue seems to be one that hasn't been brought up yet. Under no circumstance can I seem to get getResource to correctly return a path location. It got to the point where I try to find the current class' path and even that is giving me a null pointer exception. Here is a list of ways I have went about it.

testProject

  • src -klass.java
  • bin -klass.class
  • resource
  • lib

     public class Klass
     { 
            public void resourceTest()
            {   
                ClassLoader.getSystemResource(Klass.class.getSimpleName() + ".class").toString(); 
                Klass.class.getClass().getResource(Klass.class.getSimpleName() + ".class").toString();
                Klass.class.getClass().getClassLoader().getResource(Klass.class.getSimpleName() + ".class").toString();   
            }
     }
    

Please let me know if I'm doing anything wrong. I've used getResource before so this is driving me crazy. Thanks!

有帮助吗?

解决方案

Klass.class.getClass() is equivalent to Class.class, since the class of Klass.class is Class. So this would be trying to load a resource in the java.lang package. You just want Klass.class.getResource().

其他提示

There is a klass.class as last row. Try to use Klass.class with capital K

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top