?
Solved

How do I load a jar file while my app is running?

Posted on 2007-09-29
9
Medium Priority
?
652 Views
Last Modified: 2008-01-09
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
Comment
Question by:rainglen
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 19986342
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
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1000 total points
ID: 19986362
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
 
LVL 7

Assisted Solution

by:maheshexp
maheshexp earned 200 total points
ID: 19986364
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 19986424
You don't need to walk through jar file if you are using URLClassloader.
 
URLClassLodare.loadClass() - will do it for you
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 19986757
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
 

Author Comment

by:rainglen
ID: 19987214
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 19987304
:-)
0
 
LVL 7

Expert Comment

by:maheshexp
ID: 19988620
Yeah that's a gr8 answer...CEHJ :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19989173
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question