Question

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!

Était-ce utile?

La solution

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().

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top