Relative Path Problem (getClass().getResource())

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • desturrr
    New Member
    • Oct 2009
    • 18

    Relative Path Problem (getClass().getResource())

    I am having diffuculties while trying to make the path of my files relative.
    I am using an interface to hold my static variables in order not to mix them with my other codes , just implementing the interface is sufficient to use the variables.

    I need to give the address of my file as a parameter, and i am making it by using absolute path of file just like below .
    >>static JLayerPlayer xLayer=new JLayerPlayer("C :/Java_Projects/Ney1/src/Sounds/plainSounds/xFile.mp3");

    But i need to make them relative paths. When i try the code below.
    >>static JLayerPlayer xLayer =new JLayerPlayer(ge tClass().getRes ource("/Sounds/plainSounds/xFile.mp3"));

    I am getting an error says you can not use non-static getClass() method inside a static class or in interfaces, is there any other way to accomplish relative path inside interfaces.

    Thanks in advance.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    You can use class literal notation rather than getClass().

    Code:
    package my.pack;
    
    import java.net.URL;
    
    public interface Foo {
        static URL url = Foo.class.getResource("data.txt");
    }

    Comment

    Working...