Jump to content
  • Advertisement
Sign in to follow this  
zappsweden

[java] How Can I get the Absolute path at Runtime? Can I instead find out the Relative Path?

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am programming a game and want to be able to run it from anywhere on the harddrive (so I do not need to install it). I need to be able to read the images, sounds and other files that are relative to my main Class. I do not know how to use the relative path (without first finding out the absolute) so I must first find out the absolute path? The game determines its absolute path at runtime by creating a URL using the ClassName of my main Class. Here is the problem. It only works if there are no blank spaces (using Windows) in the path, since blank spaces obviously get thrown away when creating the URL. What to do? [Edited by - zappsweden on November 24, 2005 1:29:06 PM]

Share this post


Link to post
Share on other sites
Advertisement
... uh?

Sorry, but I don't understand your question. In case you wish to find out at runtime what is the absolute path you're running on, you can print it out by calling:

System.out.println(new java.io.File("").getAbsolutePath());


If you wish to load resources using relative paths, you must add the resources' folder to the classpath. Then you use a String like "res/images/myImage.jpg" to retrieve an URL with:

MyClass.class.getResource("res/images/myImage.jpg");


If that's not what you want, sorry. I couldn't understand your question ;)

Son Of Cain

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try System.getProperty("user.dir");

Share this post


Link to post
Share on other sites
> It only works if there are no blank spaces (using Windows) in the path, since blank spaces obviously get thrown away when creating the URL.

usually URLs are used if you get file from the net. If you get them from your hard drive just use the File class - it's not going to remove the spaces

Share this post


Link to post
Share on other sites
Quote:
Original post by Son of Cain
... uh?

Sorry, but I don't understand your question. In case you wish to find out at runtime what is the absolute path you're running on, you can print it out by calling:

System.out.println(new java.io.File("").getAbsolutePath());


If you wish to load resources using relative paths, you must add the resources' folder to the classpath. Then you use a String like "res/images/myImage.jpg" to retrieve an URL with:

MyClass.class.getResource("res/images/myImage.jpg");


If that's not what you want, sorry. I couldn't understand your question ;)

Son Of Cain


getAbsolutePath worked like a charm, THANKS dude :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!