Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java relative path of a static class

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Java relative path of a static class

    Hello everyone!

    For my 'tanks' game which I am developing in Java I need to read a text file in which the map's data is stored.
    I made a new class for this called 'MapReader'.
    However, I would like to be able to use the 'read' function of the class without instantiating a new MapReader object.
    That's why I made it a static class.

    Now my problem is that I can't seem to find a way to get the relative path which is needed to load the file.
    If it weren't static I would just be able to use new File(getClass().getResource("/maps/map.txt"));


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java relative path of a static class

    You can use the .class keyword to get the Class object of a given class, eg
    MapReader.class.getResource(...)

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Java relative path of a static class

    Thank you for your answer Copeg!

    I've already tried your solution and thought it was wrong.
    Now I realise I was looking at it the wrong way and that it actually works.. (:

    But the reason it wouldn't work was that I had a space character in the path!

    If I printed out the path it would replace the space character by a '%20' (hex for 32, which is it's ASCII code!)

    I solved the problem by simply removing the space character.
    But.. if I really wanted to keep the space for some reason, would the best solution be finding the path this way and then converting the %20 to a space character?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java relative path of a static class

    would the best solution be finding the path this way and then converting the %20 to a space character?
    Don't know the answer off the top of my head, but will recommend naming of resources stick to a certain convention similar to package and class naming to avoid these issues: for instance only certain characters allowed, replacing spaces with underscores, etc...

Similar Threads

  1. [SOLVED] Get path of current executing class
    By Andrew R in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 25th, 2013, 11:08 AM
  2. absolute and relative path
    By viper_pranish in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 20th, 2013, 10:34 AM
  3. File not found in class path
    By Jeff.Steinkamp in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 1st, 2013, 08:37 PM
  4. Read filein java using relative path
    By chinninani in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 20th, 2012, 10:05 PM
  5. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 10:17 AM