How do I package up an applet with multiple .jar libraries?

mag725 picture mag725 · May 13, 2010 · Viewed 17.4k times · Source

I'm creating a Java applet from a large scale pre-existing project (Vizster). I am using NetBeans 6.7.1 with JDK 1.5 on Mac OS X.

I am attempting to run the applet from it's single output .jar file, but when I do this, it says "applet loaded" at the bottom of screen in Firefox, and there is nothing in the java console, but nothing shows up in the window for the applet. I have had different errors in Firefox previous to this, including errors such as "appletNotLoaded:ClassDefNotFoundError" and also security errors, but there is never any output in my java console. Here is the html file for the applet:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <applet codebase ="." code="zuve.ZuveApplet.class"
            archive="ZuveApplet.jar"
            height="1000" width="1000"/>
  </body>
</html>

Where zuve.ZuveApplet.class is where my main method is located, and "ZuveApplet.jar" is the name of the output jar file. Here is ZuveApplet.java, the main method class:

package zuve;

import vizster.Vizster;
import vizster.VizsterLib;
import java.applet.Applet;

 public class ZuveApplet extends Applet {
     public static final String DEFAULT_START_UID = "186297";

     @Override
     public void init() {
        new Vizster();
     }

    public static void main(String[] argv) {
        VizsterLib.setLookAndFeel();
        //String startUID = argv.length > 0 ? argv[0] : DEFAULT_START_UID;
        String startUID = DEFAULT_START_UID;
        String file = argv.length > 0 ? argv[0] : null;
        new Vizster(startUID, file);
    }
 }

The applet runs perfectly well as a standalone (not embedded in html), but I need to embed it. The "Vizster" object is an extension of a JFrame, so I figure I should just be able to create an instance of it and add it to an applet. Might it be much more complicated than this?

I'm new to java and applets in general unfortunately. I have seen a lot of forum posts about source tree structure being an issue, so:

1)is it a problem that I am using multiple packages? They are all in the src directory of my project.

2)is there anything I need to be placing in my java home directory? I know a lot of people have classpath issues, but I am using a modern IDE which I thought took care of all that for me.

3)when importing my project into a NetBeans Java Web Application project, should I be adding the applet to the project as a .jar, or should I add the whole project?

4)Originally when I had created this applet, I just had my few source files and a bunch of .jar libraries as dependencies, but when I inspected the output .jar, all I saw were the compiled source files. There was no trace of the files from the libraries. Is this how it should be? I noticed that if I moved the output .jar from its containing folder, it could no longer run standalone. I thought that .jars were supposed to be self contained, is this not true? Is there anything I should know about making an executable jar?

5)on a side note, does the size of the applet denoted in the applet tags in the html have to exactly match the size of the applet itself?

Sorry for the enormous post, and the incredibly vague problem, I am working with a team in which nobody knows anything about applets or Java (real brilliant of us, I know). Any sort of help or general suggestions would really help.

Thank you!

Answer

Devon_C_Miller picture Devon_C_Miller · May 13, 2010

You can specify multiple jars in the archive attribute:

<applet codebase ="." code="zuve.ZuveApplet.class"
        archive="ZuveApplet.jar,thing.jar,anotherThing.jar"
        height="1000" width="1000"/>

A JFrame is a top-level container, so you won't be able to add it to your applet. You can have your applet create an instance of your object and let it open its own window. A more flexible approach would be to refactor your object into a JPanel. As a JPanel, it can be added to a JApplet, or to a JFrame if you also want to support running it as an application.

Also take a look at JNLP as that lets you deploy your code as an applet or as an application and also provides APIs for printing and local file access. Functionality not available to simple applets.