• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

loading images using relative path

 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ----
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic