tags:

views:

419

answers:

2

I'm new to Java (I come from an ActionScript background) and I'd like to know if there's a way to download and monitor the download progress of remote jar files inside an applet, then use the classes and other resources that are inside the downloaded jar. I know there's a JarURLConnection class, but I could not find a way to monitor the download progress through this class.

The reason why I need to do this is because I'm trying to make a JavaFX applet divided into modules, downloadable on demand.

A: 

You could define a custom ClassLoader and use this for your remote modules. This will let you control exactly how the class bytes are transferred to your applet. If you want to load entire JAR files at a time, you could make your custom class loader download JAR files when needed and unpack them with JarInputStream.

markusk
A: 

The default behaviour downloading all Jar files is usually acceptable, it downloads and shows a progress bar, but it sounds like you have a requirement to split things up. AFAIK, manually downloading classes and adding to a classpath (or custom class loaders) can't be done without signing your applet which is a path I guess you don't want to follow.

You might want to take a look at lazy downloading, available since Java 1.6 update 10.

Without knowing your exact requirements, my general advice on how you could split it would be to download all class files in the initial Jar and download resources (images / sounds / properties) as required.

Code for how to download from within an applet:

    URL url = new URL(getCodeBase(), filename);
    URLConnection urlConnection = url.openConnection();
    BufferedInputStream bufferedInputStream = null;
    try {
        bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
        // ... input data
    } finally {
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
    }
Pool
I've also found a few more details about lazy loading here: http://pscode.org/jws/api.html#ds
facildelembrar