1

Is it possible to have an Applet use a local jar?

My Applet application needs to have some dependencies ( 66Mb worth of jars). The user can install the jars previously, but how can I use them from the applet?

I can have them saved to default locations c:/myApp and /usr/local/myApp

I tried loading them with:

ClassLoader loader = URLClassLoader.newInstance(
                    new URL[]{new URL("file://" + path + "/xuggle-xuggler-5.2.jar")},
                    JNLP2Manager.getCurrentManager().getAppletClassLoader()
            );

            Thread.currentThread().setContextClassLoader(loader);

But the jar does not get automatically added to classpath, I mean, I still have to load each class individually.

Doing the following works:

 Class cls = loader.loadClass("com.xuggle.xuggler.video.ConverterFactory");
            String testString = ConverterFactory.XUGGLER_ARGB_32;

But can I have all the classes added to the applet loader?

P.S. I know I shouldn't be using Applet , but Applet is still the best fit for my kind of application.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Johnny Everson
  • 8,343
  • 7
  • 39
  • 75
  • 1
    +1 for an interesting question, and mentioning in advance that you want to stick with an applet. What is it about the app. that makes an applet so well suited? (It seems you will be having to jump through some hoops to get it working - see AlexR's point 1.) Are you aware that applets deployed using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) can download Jars lazily (only when they are needed)? – Andrew Thompson Mar 20 '12 at 15:49
  • It's to a very strict customer base and, when it's time to use, everyone should be ready to start right away because it as an real time app. I am aware of (1) but the users will have time (to install) before actually using the app. So, the point that make Applet so suited ( I actually started using JWS), is that the application has multiple Tabs, each being some kind of context. Making the user switch between the web app and the local so it can select the context which he intends the applet to work that moment, was very confusing. – Johnny Everson Mar 20 '12 at 17:18

1 Answers1

2

No, it is impossible directly at least for unsigned applets. The applet classpath is defined relatively to its codebase that is typically where applet is downloaded from.

Here are possible workarounds

  1. For signed applets you can use your custom class loader (use UrlClassLoader) that reads classes from local jar.
  2. For unsigned applets you can upload the local jar to the server and then use it from there. I think it is not so bad: from user point of view he just "helps" the application to find where the custom jar is. What really happens is that the jar is uploaded to the server and applet reads it from there. This way can be implemented using regular HTML form with input tag of type file. Using AJAX can hide a lot of details from user.
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • So, I am talking about a signed Applet. So, UrlClassLoader is what I wanted. Thanks. – Johnny Everson Mar 20 '12 at 17:06
  • So, turns out that UrlClassLoader does not put the class in the applet's classloader. It does not give error when loading, but gives me class not found when trying to use it. – Johnny Everson Mar 21 '12 at 14:33
  • Something is wrong with your code. You have to load class using your custom class loader. Something like this: `new URLClassLoader(new URL[] {new URL("file://c/custom.jar")}).loadClass("com.mycompany.MyClass").newInstance();` – AlexR Mar 21 '12 at 14:40
  • btw, should I had just kept the question answered and created other question? If so, I can fix that. Anyway, thanks. – Johnny Everson Mar 21 '12 at 14:41
  • The problem is that I'm trying to satisfy a java library, with lots of class, I cannot, at least not easily, know all classes that needs to be loaded individually. – Johnny Everson Mar 21 '12 at 14:43
  • I see. I think you should create something like library loader. It will be loaded itself by your custom class loader and will call the library directly. It should work. This way all multi-class loader systems work (e.g app. servers) – AlexR Mar 21 '12 at 15:03