File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class Loader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Loader" Watch "Class Loader" New topic
Author

Class Loader

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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?


Ping & DNS - my free Android networking tools app
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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

Joined: Jan 24, 2006
Posts: 802
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41155
    
  45
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Class Loader
 
Similar Threads
help on running jar
running jar appz
The Request Resource is not available.
Problems running jar file
use of fn:trim