Problem creating / using custom java applet
Hi
I am having trouble in both JSC2 and Netbean5.5 adding a custom java applet to my project. More accurately I can add / create the new java applet file and compile my code perfectly but when I try to use the applet from within a jsp or page fragment the browser throws a classnotfound error.
As an example:
A. I create a java applet to print a string to the browser window. I compile the applet fine by right clicking it and selecting compile... that bit is fine..
B. In a jps I create a static text, switch off escape and add the following into the text part : <applet code="myApplet.class" width="200" height="200"/>
Section B is obviously where my problem is.. when I run the project everything works fine except for the applet - the browser states (in the java console) a ClassNotFoundException.
How can this be when I created the applet, compiled the applet and did everything through the IDE.. shouldn't my jsp be able to find it automatically with the code="myApplet.class" statement.
I know I am leading myself astray but can anyone tell me where.
Thanks in advance...
Message was edited by:
jasonweise
# 1
Hi There,Which directory did you put the applets's .class file?Im not sure if you have seen this techtip, but this does what you are looking for http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/appl et.htmlHope it helpsK
# 2
Hi
I didn't place the .class file in any directory. The class file is created and compiled within JSC (even tried NetBeans 5.5 with VWP) so I presumed the IDE would be smart enought to output the compiled .class file to the correct location.
Now i have even more questions..
1. Do neither JSC or NetBeans 5.5 place the .class file in the correct location after compilation?
2. Do I still have to manually move it to the web directory?
3. And if so where does JSC place the .class file after I compile the applet so I can find it and move it?
4. Is there configuration options to make JSC / NB5.5 automatically copy the .class file to the required directory?
If it does have to be all done manually it just seems a little couter-productive is all.
Thanks so much for your assistance thus far.. :)
# 3
Hi There,well the IDE does put the class file in the right place, but just to be sure the exception is not due to that. Can you post the stack trace you get. ThanksK
# 4
Hi again
Here is the output of the java console:
-
load: class myApplet.class not found.
java.lang.ClassNotFoundException: myApplet.class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 10 more
-
The applet is just a tiny test applet at this stage and is supposed to just output the word test. Here is the code:
-
* Created on 26 January 2007, 08:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package webapplication2;
import java.applet.Applet;
import java.awt.Graphics;
/**
*
* @author User
*/
public class myApplet extends Applet implements Runnable {
/** Creates a new instance of myApplet */
public myApplet() {
}
public void run()
{
repaint();
}
public void paint(Graphics g)
{
g.drawString("Test",0,0);
}
}
--
And to invoke this applet I am adding a static text to the page, turning off escaping and adding the following to the text property:
<applet code="myApplet.class" height="200" width="200" />
And I am compiling the myApplet.java file from within the IDE.. this has REALLY got me stumped.
Does exactly the same thing in JSC and NB5.5 so I am sure it is something I am missing....
PS. I realize that that this applet doesn't need to implement Runnable but the applet it will eventually turn into does need to create a timer thread so needs to be runnable.. just FYI in case you were wondering.
# 5
Hi There,
I tried the instructions in the techtip and when I ran the application, I also got the same error as you.
But then I looked around to find this FAQ which explains why.
http://developers.sun.com/prodtech/javatools/jscreator/reference/faqs/technical /components/add_applet_package.html
That should help you
Thanks
K
# 6
Thank you so much... one last question.. do you have a sample of a "servlet that will take a relative path to requested resource as a parameter and will stream the resource back" (In this case a java applet) and how it would be used.
I don't understand how the servlet will stream back a java applet to the page.
Thanks again.
# 7
Does anyone have a servlet example on using java applets...?Is it a better option just to copy the compiled .class file from the WEB-INF folder to the web folder.?
# 8
Hi there
Here is what I did to get the applet to work, by putting the .class file outside the web-inf directory.
I didnt find a automatic way of making Creator do this. Had to do it manually.
Putting the applets's .class file outside web-inf seems to be expected behaviour rather than a hack , see this
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WebComponents3.html (see client side classes)
Im using the clock applet which is described in the instructions at http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/appl et.html)
Build your project in Creator. Go to the directory where creator stores your project. Open the directory with the name of your Project say "AppletTest1".
Under the 'build' directory, open 'web' directory and copy the Clock.class file into it.
Now go back to Creator refresh all files , build and run the project. Now it should work, and also if you view the war file it should have the .class file outside the web-inf folder.
Also it looks like there is no readily available example which demonstrates how to do what suggested in the FAQ about
"serving a resource from under WEB-INF to a browser - to have a servlet that will take a relative path to requested resource as a parameter and will stream the resource back."
Will have to try this out.
Hope it helps
Thanks
K
