rainglen
asked on
How do I load a jar file while my app is running?
I need a simple solution for loading jars that contain additional image and audio files while my applet/application is already running. The ideal solution will probably be to create a manager to check if a jar has already been loaded, butfor right now I need the code that will do the load. I don't know if the load should be something I do from the app logic by anticipating when the resources will be needed, or should the load be embedded in the following code when the attempt is made to load the resource.
My image loading code currently looks like this:
URL url = getClass().getResource(fil ename);
BufferedImage sourceImage = ImageIO.read(url);
GraphicsConfiguration gc = GraphicsEnvironment.getLoc alGraphics Environmen t().getDef aultScreen Device().
getDefaultConfiguration();
Image image = gc.createCompatibleImage(s ourceImage .getWidth( ), sourceImage.getHeight(),
Transparency.BITMASK);
image.getGraphics().drawIm age(source Image, 0, 0, null);
My audio loading code currently looks like this:
URL url = getClass().getResource(fil ename);
AudioInputStream audioStream = AudioSystem.getAudioInputS tream(url) ;
// get the number of bytes to read
int length = (int) (audioStream.getFrameLengt h() * audioStream.getFormat().ge tFrameSize ());
// read the entire stream
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStrea m);
try {
is.readFully(samples);
is.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
By the way, code to unload jars to free up memory would be much appreciated as well.
My image loading code currently looks like this:
URL url = getClass().getResource(fil
BufferedImage sourceImage = ImageIO.read(url);
GraphicsConfiguration gc = GraphicsEnvironment.getLoc
getDefaultConfiguration();
Image image = gc.createCompatibleImage(s
Transparency.BITMASK);
image.getGraphics().drawIm
My audio loading code currently looks like this:
URL url = getClass().getResource(fil
AudioInputStream audioStream = AudioSystem.getAudioInputS
// get the number of bytes to read
int length = (int) (audioStream.getFrameLengt
// read the entire stream
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStrea
try {
is.readFully(samples);
is.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
By the way, code to unload jars to free up memory would be much appreciated as well.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You don't need to walk through jar file if you are using URLClassloader.
URLClassLodare.loadClass() - will do it for you
URLClassLodare.loadClass()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, everybody, for your very helpful comments! I I will put them to use.
Note to sciuriware, I am making separate jars, because it's 40+ meg total and will be growing linearly indefinitely, so to save initial download size, I want to be able to load some jars only if and when needed.
Note to sciuriware, I am making separate jars, because it's 40+ meg total and will be growing linearly indefinitely, so to save initial download size, I want to be able to load some jars only if and when needed.
:-)
Yeah that's a gr8 answer...CEHJ :-)
:-)
1) put them in the primary jar as resource.
2) open and load them as necessary.
I don't see the advantage of an extra jar for data files.
;JOOP!