Link to home
Start Free TrialLog in
Avatar of rainglen
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(filename);
      BufferedImage sourceImage = ImageIO.read(url);
    GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().
        getDefaultConfiguration();
    Image image = gc.createCompatibleImage(sourceImage.getWidth(), sourceImage.getHeight(),
                                           Transparency.BITMASK);
    image.getGraphics().drawImage(sourceImage, 0, 0, null);

My audio loading code currently looks like this:
      URL url = getClass().getResource(filename);
      AudioInputStream audioStream = AudioSystem.getAudioInputStream(url);

      // get the number of bytes to read
      int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize());

    // read the entire stream
    byte[] samples = new byte[length];
    DataInputStream is = new DataInputStream(audioStream);
    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.
Avatar of sciuriware
sciuriware

Why should additional files be in an extra .jar?
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!
ASKER CERTIFIED SOLUTION
Avatar of Ajay-Singh
Ajay-Singh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You don't need to walk through jar file if you are using URLClassloader.
 
URLClassLodare.loadClass() - will do it for you
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rainglen

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.
:-)
Yeah that's a gr8 answer...CEHJ :-)
:-)