users@glassfish.java.net

Re: ClassNotFoundException when serving applet

From: <glassfish_at_javadesktop.org>
Date: Tue, 04 Dec 2007 11:08:28 PST

Hi,

The applet class file is executed on the client browser, so it should be downloadable as a normal resources (like images, HTML pages, etc.)

But, if you are using NetBeans 6, and the source code of the "servlet" and the "applet" are in the same WEB project, you are probably generating all the classes in "/WEB-INF/classes" folder of the WAR archive (that Java web servers doesn't allow to download, for security reason).

There are many solutions:
1) Copy the compiled applet class into a public folder with the same name of the class' namespace ("yourWebProjectDir/web/eg"). But this "solution" will not automatically update the published applet class file on compilation of code changes (unless you edit "build.xml" project file). In this case, you could use the next "applet" tag options:
<applet code=”eg.EgApplet.class” codebase="/EnterpriseApplication1-war" ...></applet>

2) Archive the compiled class into a "jar" file (recommended). This solution can also be configured with NetBeans to automatically update published classes on recompilation of code changes: Create another Java Library project with the applet class (and dependencies), then create a public folder in the WEB project like "yourProjectDir/web/applets", and finally, configure the WEB project to include the generated library "jar" as a public resource (open WEB project properties --> Build/Packaging section --> Add project --> Select the previous java library to insert the generated library "jar" into the WAR Content into the public folder, and also set the value "applets" into the "Path in WAR" column). After that, when you rebuild/deploy the "WAR project", it will compile the "java library project" with the applet (when necessary), and will publish the generated "jar" into the "applets" public folder of the WEB project. In this case, you can use the following tag options:
<applet code=”eg.EgApplet.class” codebase="/EnterpriseApplication1-war/applets" archive="yourJavaLibrary.jar" ...></applet>

Final notes: if you also want to execute the applet class on IE browsers with old "Microsoft VM", you should compile the class with JDK 1.1 (JDK 1.2 may also work), and don't use compression when generating the java library "jar" archive (java library project properties --> Build/Packaging section --> uncheck "compress JAR file" option).


I hope it works for your applet.
[Message sent by forum member 'jmarine' (jmarine)]

http://forums.java.net/jive/thread.jspa?messageID=248481