getClassLoader().getResource() returns null









up vote
11
down vote

favorite
3












I have this test app:



import java.applet.*;
import java.awt.*;
import java.net.URL;
public class Test extends Applet


public void init()

URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
System.out.println(some.toString());
System.out.println(some.getFile());
System.out.println(some.getPath());





When I run it from Eclipse, I get the error:



java.lang.NullPointerException
at Test.init(Test.java:9)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


Classpath (from .CLASSPATH file)



<classpathentry kind="src" path="src"/>


In my c:projectsrc folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.



What am I doing wrong and how to resolve it?










share|improve this question



























    up vote
    11
    down vote

    favorite
    3












    I have this test app:



    import java.applet.*;
    import java.awt.*;
    import java.net.URL;
    public class Test extends Applet


    public void init()

    URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
    System.out.println(some.toString());
    System.out.println(some.getFile());
    System.out.println(some.getPath());





    When I run it from Eclipse, I get the error:



    java.lang.NullPointerException
    at Test.init(Test.java:9)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    Classpath (from .CLASSPATH file)



    <classpathentry kind="src" path="src"/>


    In my c:projectsrc folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.



    What am I doing wrong and how to resolve it?










    share|improve this question

























      up vote
      11
      down vote

      favorite
      3









      up vote
      11
      down vote

      favorite
      3






      3





      I have this test app:



      import java.applet.*;
      import java.awt.*;
      import java.net.URL;
      public class Test extends Applet


      public void init()

      URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
      System.out.println(some.toString());
      System.out.println(some.getFile());
      System.out.println(some.getPath());





      When I run it from Eclipse, I get the error:



      java.lang.NullPointerException
      at Test.init(Test.java:9)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)


      Classpath (from .CLASSPATH file)



      <classpathentry kind="src" path="src"/>


      In my c:projectsrc folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.



      What am I doing wrong and how to resolve it?










      share|improve this question















      I have this test app:



      import java.applet.*;
      import java.awt.*;
      import java.net.URL;
      public class Test extends Applet


      public void init()

      URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
      System.out.println(some.toString());
      System.out.println(some.getFile());
      System.out.println(some.getPath());





      When I run it from Eclipse, I get the error:



      java.lang.NullPointerException
      at Test.init(Test.java:9)
      at sun.applet.AppletPanel.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)


      Classpath (from .CLASSPATH file)



      <classpathentry kind="src" path="src"/>


      In my c:projectsrc folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.



      What am I doing wrong and how to resolve it?







      java applet






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 15:41









      user1803551

      9,71852951




      9,71852951










      asked Mar 9 '09 at 10:20









      Click Upvote

      95.3k222509688




      95.3k222509688






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          8
          down vote



          accepted










          I would do it this way:



          final InputStream stream;

          stream = Test.class.getResourceAsStream("assets/pacman.png");
          System.out.println("Stream = " + stream);


          "/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.






          share|improve this answer






















          • Your code gives the output "Stream = null" :(
            – Click Upvote
            Mar 9 '09 at 10:35










          • as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
            – TofuBeer
            Mar 9 '09 at 10:39










          • The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
            – Click Upvote
            Mar 9 '09 at 10:43






          • 1




            the assests dir needs to be where the .class file is
            – TofuBeer
            Mar 9 '09 at 10:51










          • glad you got it working
            – TofuBeer
            Mar 9 '09 at 10:53

















          up vote
          18
          down vote













          You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.



          In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.



          In other words, try either of these lines:



          URL viaClass=Test.class.getResource("/assets/pacman.png");
          URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");





          share|improve this answer




















          • Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
            – Click Upvote
            Mar 9 '09 at 10:32










          • that measn that "some" is null... which means you are not getting the resource properly.
            – TofuBeer
            Mar 9 '09 at 10:34










          • yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
            – Click Upvote
            Mar 9 '09 at 10:37










          • Try getting the applet side of things out of the equation - run the same code in a console app.
            – Jon Skeet
            Mar 9 '09 at 10:39










          • But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
            – Click Upvote
            Mar 9 '09 at 10:41

















          up vote
          4
          down vote













          Click Upvote,



          • When you use .getClass().getResource(fileName) it considers the
            location of the fileName is the same location of the of the calling
            class.

          • When you use .getClass().getClassLoader().getResource(fileName) it
            considers the location of the fileName is the root - in other words
            bin folder

          It hits NullPointerException if the file is actually not exist there.



          Source:



          package Sound;
          public class ResourceTest
          public static void main(String args)
          String fileName = "Kalimba.mp3";
          System.out.println(fileName);
          System.out.println(new ResourceTest().getClass().getResource(fileName));
          System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));


          OutPut ;



          Kalimba.mp3
          file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
          file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3







          share|improve this answer



























            up vote
            1
            down vote













            This works for me:




            URL viaClass=Test.class.getResource("assets/test.html");




            which assets in the same folder with Test.class output file (after a miserable checking and debugging)






            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f625687%2fgetclassloader-getresource-returns-null%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              8
              down vote



              accepted










              I would do it this way:



              final InputStream stream;

              stream = Test.class.getResourceAsStream("assets/pacman.png");
              System.out.println("Stream = " + stream);


              "/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.






              share|improve this answer






















              • Your code gives the output "Stream = null" :(
                – Click Upvote
                Mar 9 '09 at 10:35










              • as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
                – TofuBeer
                Mar 9 '09 at 10:39










              • The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
                – Click Upvote
                Mar 9 '09 at 10:43






              • 1




                the assests dir needs to be where the .class file is
                – TofuBeer
                Mar 9 '09 at 10:51










              • glad you got it working
                – TofuBeer
                Mar 9 '09 at 10:53














              up vote
              8
              down vote



              accepted










              I would do it this way:



              final InputStream stream;

              stream = Test.class.getResourceAsStream("assets/pacman.png");
              System.out.println("Stream = " + stream);


              "/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.






              share|improve this answer






















              • Your code gives the output "Stream = null" :(
                – Click Upvote
                Mar 9 '09 at 10:35










              • as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
                – TofuBeer
                Mar 9 '09 at 10:39










              • The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
                – Click Upvote
                Mar 9 '09 at 10:43






              • 1




                the assests dir needs to be where the .class file is
                – TofuBeer
                Mar 9 '09 at 10:51










              • glad you got it working
                – TofuBeer
                Mar 9 '09 at 10:53












              up vote
              8
              down vote



              accepted







              up vote
              8
              down vote



              accepted






              I would do it this way:



              final InputStream stream;

              stream = Test.class.getResourceAsStream("assets/pacman.png");
              System.out.println("Stream = " + stream);


              "/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.






              share|improve this answer














              I would do it this way:



              final InputStream stream;

              stream = Test.class.getResourceAsStream("assets/pacman.png");
              System.out.println("Stream = " + stream);


              "/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 13 '09 at 17:29









              Carl Manaster

              32.6k1481135




              32.6k1481135










              answered Mar 9 '09 at 10:34









              TofuBeer

              49.4k12104149




              49.4k12104149











              • Your code gives the output "Stream = null" :(
                – Click Upvote
                Mar 9 '09 at 10:35










              • as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
                – TofuBeer
                Mar 9 '09 at 10:39










              • The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
                – Click Upvote
                Mar 9 '09 at 10:43






              • 1




                the assests dir needs to be where the .class file is
                – TofuBeer
                Mar 9 '09 at 10:51










              • glad you got it working
                – TofuBeer
                Mar 9 '09 at 10:53
















              • Your code gives the output "Stream = null" :(
                – Click Upvote
                Mar 9 '09 at 10:35










              • as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
                – TofuBeer
                Mar 9 '09 at 10:39










              • The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
                – Click Upvote
                Mar 9 '09 at 10:43






              • 1




                the assests dir needs to be where the .class file is
                – TofuBeer
                Mar 9 '09 at 10:51










              • glad you got it working
                – TofuBeer
                Mar 9 '09 at 10:53















              Your code gives the output "Stream = null" :(
              – Click Upvote
              Mar 9 '09 at 10:35




              Your code gives the output "Stream = null" :(
              – Click Upvote
              Mar 9 '09 at 10:35












              as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
              – TofuBeer
              Mar 9 '09 at 10:39




              as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
              – TofuBeer
              Mar 9 '09 at 10:39












              The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
              – Click Upvote
              Mar 9 '09 at 10:43




              The .class is put in c:javatestbin when i hit run in eclipse. The .java is in c:javatestsrc and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
              – Click Upvote
              Mar 9 '09 at 10:43




              1




              1




              the assests dir needs to be where the .class file is
              – TofuBeer
              Mar 9 '09 at 10:51




              the assests dir needs to be where the .class file is
              – TofuBeer
              Mar 9 '09 at 10:51












              glad you got it working
              – TofuBeer
              Mar 9 '09 at 10:53




              glad you got it working
              – TofuBeer
              Mar 9 '09 at 10:53












              up vote
              18
              down vote













              You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.



              In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.



              In other words, try either of these lines:



              URL viaClass=Test.class.getResource("/assets/pacman.png");
              URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");





              share|improve this answer




















              • Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
                – Click Upvote
                Mar 9 '09 at 10:32










              • that measn that "some" is null... which means you are not getting the resource properly.
                – TofuBeer
                Mar 9 '09 at 10:34










              • yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
                – Click Upvote
                Mar 9 '09 at 10:37










              • Try getting the applet side of things out of the equation - run the same code in a console app.
                – Jon Skeet
                Mar 9 '09 at 10:39










              • But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
                – Click Upvote
                Mar 9 '09 at 10:41














              up vote
              18
              down vote













              You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.



              In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.



              In other words, try either of these lines:



              URL viaClass=Test.class.getResource("/assets/pacman.png");
              URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");





              share|improve this answer




















              • Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
                – Click Upvote
                Mar 9 '09 at 10:32










              • that measn that "some" is null... which means you are not getting the resource properly.
                – TofuBeer
                Mar 9 '09 at 10:34










              • yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
                – Click Upvote
                Mar 9 '09 at 10:37










              • Try getting the applet side of things out of the equation - run the same code in a console app.
                – Jon Skeet
                Mar 9 '09 at 10:39










              • But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
                – Click Upvote
                Mar 9 '09 at 10:41












              up vote
              18
              down vote










              up vote
              18
              down vote









              You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.



              In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.



              In other words, try either of these lines:



              URL viaClass=Test.class.getResource("/assets/pacman.png");
              URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");





              share|improve this answer












              You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.



              In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.



              In other words, try either of these lines:



              URL viaClass=Test.class.getResource("/assets/pacman.png");
              URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 9 '09 at 10:26









              Jon Skeet

              1067k66778258366




              1067k66778258366











              • Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
                – Click Upvote
                Mar 9 '09 at 10:32










              • that measn that "some" is null... which means you are not getting the resource properly.
                – TofuBeer
                Mar 9 '09 at 10:34










              • yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
                – Click Upvote
                Mar 9 '09 at 10:37










              • Try getting the applet side of things out of the equation - run the same code in a console app.
                – Jon Skeet
                Mar 9 '09 at 10:39










              • But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
                – Click Upvote
                Mar 9 '09 at 10:41
















              • Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
                – Click Upvote
                Mar 9 '09 at 10:32










              • that measn that "some" is null... which means you are not getting the resource properly.
                – TofuBeer
                Mar 9 '09 at 10:34










              • yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
                – Click Upvote
                Mar 9 '09 at 10:37










              • Try getting the applet side of things out of the equation - run the same code in a console app.
                – Jon Skeet
                Mar 9 '09 at 10:39










              • But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
                – Click Upvote
                Mar 9 '09 at 10:41















              Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
              – Click Upvote
              Mar 9 '09 at 10:32




              Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
              – Click Upvote
              Mar 9 '09 at 10:32












              that measn that "some" is null... which means you are not getting the resource properly.
              – TofuBeer
              Mar 9 '09 at 10:34




              that measn that "some" is null... which means you are not getting the resource properly.
              – TofuBeer
              Mar 9 '09 at 10:34












              yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
              – Click Upvote
              Mar 9 '09 at 10:37




              yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
              – Click Upvote
              Mar 9 '09 at 10:37












              Try getting the applet side of things out of the equation - run the same code in a console app.
              – Jon Skeet
              Mar 9 '09 at 10:39




              Try getting the applet side of things out of the equation - run the same code in a console app.
              – Jon Skeet
              Mar 9 '09 at 10:39












              But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
              – Click Upvote
              Mar 9 '09 at 10:41




              But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
              – Click Upvote
              Mar 9 '09 at 10:41










              up vote
              4
              down vote













              Click Upvote,



              • When you use .getClass().getResource(fileName) it considers the
                location of the fileName is the same location of the of the calling
                class.

              • When you use .getClass().getClassLoader().getResource(fileName) it
                considers the location of the fileName is the root - in other words
                bin folder

              It hits NullPointerException if the file is actually not exist there.



              Source:



              package Sound;
              public class ResourceTest
              public static void main(String args)
              String fileName = "Kalimba.mp3";
              System.out.println(fileName);
              System.out.println(new ResourceTest().getClass().getResource(fileName));
              System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));


              OutPut ;



              Kalimba.mp3
              file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
              file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3







              share|improve this answer
























                up vote
                4
                down vote













                Click Upvote,



                • When you use .getClass().getResource(fileName) it considers the
                  location of the fileName is the same location of the of the calling
                  class.

                • When you use .getClass().getClassLoader().getResource(fileName) it
                  considers the location of the fileName is the root - in other words
                  bin folder

                It hits NullPointerException if the file is actually not exist there.



                Source:



                package Sound;
                public class ResourceTest
                public static void main(String args)
                String fileName = "Kalimba.mp3";
                System.out.println(fileName);
                System.out.println(new ResourceTest().getClass().getResource(fileName));
                System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));


                OutPut ;



                Kalimba.mp3
                file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
                file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3







                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  Click Upvote,



                  • When you use .getClass().getResource(fileName) it considers the
                    location of the fileName is the same location of the of the calling
                    class.

                  • When you use .getClass().getClassLoader().getResource(fileName) it
                    considers the location of the fileName is the root - in other words
                    bin folder

                  It hits NullPointerException if the file is actually not exist there.



                  Source:



                  package Sound;
                  public class ResourceTest
                  public static void main(String args)
                  String fileName = "Kalimba.mp3";
                  System.out.println(fileName);
                  System.out.println(new ResourceTest().getClass().getResource(fileName));
                  System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));


                  OutPut ;



                  Kalimba.mp3
                  file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
                  file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3







                  share|improve this answer












                  Click Upvote,



                  • When you use .getClass().getResource(fileName) it considers the
                    location of the fileName is the same location of the of the calling
                    class.

                  • When you use .getClass().getClassLoader().getResource(fileName) it
                    considers the location of the fileName is the root - in other words
                    bin folder

                  It hits NullPointerException if the file is actually not exist there.



                  Source:



                  package Sound;
                  public class ResourceTest
                  public static void main(String args)
                  String fileName = "Kalimba.mp3";
                  System.out.println(fileName);
                  System.out.println(new ResourceTest().getClass().getResource(fileName));
                  System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));


                  OutPut ;



                  Kalimba.mp3
                  file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3
                  file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 29 '11 at 0:51









                  namalfernandolk

                  4,62984078




                  4,62984078




















                      up vote
                      1
                      down vote













                      This works for me:




                      URL viaClass=Test.class.getResource("assets/test.html");




                      which assets in the same folder with Test.class output file (after a miserable checking and debugging)






                      share|improve this answer
























                        up vote
                        1
                        down vote













                        This works for me:




                        URL viaClass=Test.class.getResource("assets/test.html");




                        which assets in the same folder with Test.class output file (after a miserable checking and debugging)






                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          This works for me:




                          URL viaClass=Test.class.getResource("assets/test.html");




                          which assets in the same folder with Test.class output file (after a miserable checking and debugging)






                          share|improve this answer












                          This works for me:




                          URL viaClass=Test.class.getResource("assets/test.html");




                          which assets in the same folder with Test.class output file (after a miserable checking and debugging)







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 26 '12 at 3:04









                          Thinhbk

                          1,9091829




                          1,9091829



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f625687%2fgetclassloader-getresource-returns-null%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              27

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              Category:Rhetoric