• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 655
  • Last Modified:

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.
0
rainglen
Asked:
rainglen
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
sciuriwareCommented:
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!
0
 
Ajay-SinghCommented:
If you know the jar file location, you can use the following to load it
 
ClassLoader loader = new URLCLassLoader(new URL[]{jarLocation}); //
jarLocation is the location of the resource jar.
 
// replace all the occurrences of getClass().getResource with
URL url = loader.getResource(filename);
0
 
maheshexpCommented:
The below piece of code will help you to load the required resource from a jar.

      String jarName = "c:/jar/jar1.jar";
 
    URLClassLoader urlLoader = getURLClassLoader(new URL("file", null, jarName));
 
    JarInputStream jis = new JarInputStream(new FileInputStream(jarName));
    JarEntry entry = jis.getNextJarEntry();

 
    while (entry != null) {
      String name = entry.getName();
      if (name.endsWith(".class")) {
        
        name = name.substring(0, name.length() - 6);
        name = name.replace('/', '.');
        System.out.print(name);
 
        try {
          urlLoader.loadClass(name);
        } catch (Throwable e) {
            }
 
      }
      entry = jis.getNextJarEntry();
    }
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Ajay-SinghCommented:
You don't need to walk through jar file if you are using URLClassloader.
 
URLClassLodare.loadClass() - will do it for you
0
 
CEHJCommented:
You can load the files directly from the jar:

URL url = new URL("jar:file:sounds.jar!/sounds/x.au");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(url);

>>By the way, code to unload jars to free up memory would be much appreciated as well.

You don't need to unload jars - they're not 'loaded' in the first place - only classes are. You only need to close ' audioStream' then set it to null if you need to prompt the garbage collector
0
 
rainglenAuthor Commented:
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.
0
 
CEHJCommented:
:-)
0
 
maheshexpCommented:
Yeah that's a gr8 answer...CEHJ :-)
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now