This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Loader

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic