Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Class Loader

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i need is to be able to include my data files and pictures with my jar
files. But when i include them into the jar, it will load the pics/data files when the jar is in same directory, but not when it is in another location.

i've heard to use classloader, but i do not know how to use it.

any help would be great (semi-detailed also)

Thanks,


Justin
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can load files contained in the jar using Class.getResourceAsStream.

But I'm not clear in what you mean by "the other directory" - if all files are cointained in the jar, which files are in a different directory?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok when i create the jar file, I put the files that are in the folder with it. Now if i try to move that jar file to desktop, when i run it, the images and datafiles wont be included in the jar. the jar will only use the images/data files if its in the same directory that i initially created that jar file in.


Justin
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Well, the getResourceAsStream approach allows you to keep those files in the jar, so the problem can't occur.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so the parameter to the function would be the String filename of the file i want to keep in the jar?

Justin
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Justin Fox:
so the parameter to the function would be the String filename of the file i want to keep in the jar?

Justin



Either put the files in the jar and load the resource as a stream or if you are unable to find the jar open a file selection window and ask the user to point to the jar location
 
Ulf Dittmer
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Justin Fox:
so the parameter to the function would be the String filename of the file i want to keep in the jar?



Yes. If the file is at the top level of the jar file, use "/filename.txt". One level deeper, "/directory/filename.txt" and so on.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic