Java – How to run applet in eclipse using web project

appletjavajsp

Hello I have tried the following code

index.jsp

<applet code="com.applet.PrintApplet" codebase ="TestApplet.jar" width="320" height="120"></applet>

java class PrintApplet.java

import java.applet.Applet;
import java.awt.Color;

public class PrintApplet extends Applet{

    public void paint(Graphics g){
      g.drawString("Welcome in Java Applet.",40,20);
   }

}

when this application runs in browser then

class not found exception is fired..

but i have problem in browser

error like a popup box with three button

Detail Ignore and Reload

Application error

classNotFoundException 

com.applet.printApplet

in Detail button

Java Plug-in 10.21.2.11
Using JRE version 1.7.0_21-b11 Java HotSpot(TM) Client VM
User home directory = C:\Users\Helthcare2
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------

and i have added jar file in build path also.

Best Solution

You have two options:
1. to use appletviewer which comes with your JDK to just see the applet work in a bare-bones browser
2. embed the applet tag in your HTML page.

<html>
    <title>My Applet</title>
    <body>
        <applet code="PrintApplet.class" width="400" height="400"></applet>
    </body>
</html>  

But as it stands, <applet> has been deprecated.
So, here is what you do:
1. Add this to your <head> : <script src="http://java.com/js/deployJava.js"></script>
2. Add this to your <body>:

<script>
    var attributes = {codebase:'http://my.url/my/path/to/codebase',
                      code:'my.main.Applet.class',
                      archive: 'my-archive.jar',
                      width: '800', 
                      height: '600'};
    var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
    var version = '1.5' ; // JDK version
    deployJava.runApplet(attributes, parameters, version);
</script>  

Again, as of HTML5, <head> is not needed so you can just type <script>....</script>

This was taken from: Embedding Java Applet into .html file accepted answer.

Related Question