Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes loading images using relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "loading images using relative path" Watch "loading images using relative path" New topic

loading images using relative path

Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Hi pals,
How can i load an image in a platform indepenedent way in an application? Like my main method is in MainApp.class that is under different package than the classes that will load the images. I tried to use "nameOfFile.gif" in the constructor but it can't load if i don't say absolute position i.e. "C:\myImages\logo.gif". I tried to make a jar from where the images will be extracted.
Here is a sample code that i tried ----

Ashik Uzzaman
Senior Software Engineer, TubeMogul, Emeryville, CA, USA.
Lars Tode
Ranch Hand

Joined: Feb 07, 2002
Posts: 45
i don't know if u can use a relative path, but here is a other idea how u can implement an image:
1. File myFile = new File("file.class");
U make an instanz of ur class itself with the File-Class.
2. StrinBuffer progPath= new StringBuffer(myFile.getAbsolutePath());
getting the absolute Path of your programm
3.1 c:\programm files\myprograms\javaapps\package\mainFile.class
remove that what u don't need
3.2 c:\programm files\myprograms\javaapps\
U work with a String, so it's not so difficult, u can use the method delete() of the class String.
3.3 adding the new path (or relative path)
4. the result is an absolute path that works on every system
This is for i.e 4 Windows-Systems
c:\programm files\myprograms\javaapps\images\yourImage.gif
Hope that's something that does what u want.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Ashik uzzaman:
I tried to use "nameOfFile.gif" in the constructor but it can't load if i don't say absolute position i.e. "C:\myImages\logo.gif".

I'm not certain I'm following you exactly, but it would seem to me that you don't want to consider the absolute position of the image. The user should be able to put the jar wherever he or she wants without concern to directory or hard drive. If you know the relative directory structure (path) to the images (which you should), then to get the file separator character(s) no matter what OS is running, use System.getProperty("file.separator").
Good Luck.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2373

Lars, thanks for some very good steps. Now I am trying to prepare a jar file that gets images from the manifest file through relative path.
Dirk, you are in the right track, but i wished to put all my images in the images directory of a jar file that also contains some other class files. I want to access these images from these class files as well as any other external classes that can use this jar.
I agree. Here's the link:
subject: loading images using relative path
It's not a secret anymore!