Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loading images using relative path

 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ----
 
Lars Tode
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
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)
progPath+"images"+System.getProperty("file.separator")+"yourImage.gif";
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.
greetx
Lars
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic