run applet in web application
Asked Answered
F

3

7

I want to run simple applet in my web application using html applet tag but it gives error like

java.lang.ClassNotFoundException: MyApplet

please, give me sample application if possible .....

Filiate answered 23/11, 2010 at 7:43 Comment(4)
Show the complete <applet> tag you have used to embed applet.Grange
<applet code="MyApplet" codebase="./WEB-INF/classes/" width="600" height="95"/>Filiate
this is the applete tag that i usedFiliate
The problem is at the codebase. Make sure it points to the class file.Grange
C
10

the problem is that the applet engine can't find your MyApplet class at the codebase you have defined.

This can be caused because you have you class at your /WEB-INF/classes directory. This directory is protected by the servlet engine, for it not to be accesed from external resources (as can be an applet tag at a JSP/HTML page.

There are a few ways to solve this. The easiest one is to pack your MyApplet class un a jar file (let's call it myapplet.jar), and save it at an accesible directory (i.e. the jsp folder of your web application). As an example, supose you have the following folders for the web application:

/MyWebApp/jsp
/MyWebApp/applet
/MyWebApp/WEB-INF

The client browsers can access the content of jsp and applet folders.

Then, save your myapplet.jar at the applet folder, and set your applet tag configuration like this (suposing that you web context is MyWebApp):

<applet codebase="/MyWebApp/applet" archive="myapplet.jar" 
        code="MyApplet.class" width="600" height="500">
</applet>

Here you can find more info about the applet tag: http://docs.oracle.com/javase/tutorial/deployment/applet/index.html

Climber answered 23/11, 2010 at 8:10 Comment(1)
Please edit your answer to include a closing applet tag and width/height attributes. The HTML shown is invalid, and it is a pity to tarnish such a good answer with trivialities like invalid HTML. Also note that many things have changed in applets (and particularly applet deployment) since 1.4.2, so it is handy to hunt down the docs. for 1.6 if possible. To that end, you might scan some of the links in the applet Wiki entry, mentioned in my other comments.Penman
H
1

Old thread, I know... but I've come up with a little hack that allows you to serve applets that are inside your WEB-INF/classes folder so that you don't need an extra jar in your project (and you can redeploy your applet a little faster). The downside of this is that you can't sign your applet (because it's a .class not a jar). Let's cut to the chase here...

First, create a little servlet that serves applets (it requires Javassist):

public class AppletServlet implements Servlet {
...
ClassPool pool = ClassPool.getDefault();

@Override
public void init(ServletConfig config) throws ServletException {
    pool.insertClassPath(new ClassClassPath(this.getClass()));
}

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    String className = ((HttpServletRequest) req).getPathInfo().substring(1);
    try {
        CtClass cc = pool.get(className.replace("/", ".").replace(".class", ""));
        res.setContentType("application/x-java-applet;version=1.5.0");
        res.setContentLength(cc.toBytecode().length);
        res.getOutputStream().write(cc.toBytecode());
        res.getOutputStream().close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
...
}

Now declare your AppletServlet (I know, terrible name) as a servlet in your web.xml:

<servlet>
    <servlet-name>Applet Servlet</servlet-name>
    <servlet-class>com.example.AppletServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Applet Servlet</servlet-name>
    <url-pattern>/applet/*</url-pattern>
</servlet-mapping>

Finally, invoke your applet from your page:

<object type="application/x-java-applet" height="300" width="550">
    <param name="codebase" value="applet/" />
    <param name="code" value="com.example.MyApplet" />
    <param name="teste" value="teste"></param>
    Applet failed to run. No Java plug-in was found.
</object>

And that's it. The servlet will use Javassist to get the byte code for your class and serve it to the request.

Disclaimer If someone knows your package structure, they could download all the classes and do evil things from there. So make sure you only allow the servlet to serve classes that are actually applets.

Harmonize answered 20/7, 2012 at 20:6 Comment(2)
Good idea but it doesn't compile - where does "pool" come from?Broadbill
My bad. Classic example of errors due to copying and pasting ;) It should work nowHarmonize
E
0

Check 2 things. 1. the codebase is correct. To check that it is correctly written compose full URL (URL of your page + codebase) and try it directly in browser. Be sure that it is correct.

  1. The class name is written correctly. It must be fully qualified class name (including package name)

If it does not work, post your tag here

Evangel answered 23/11, 2010 at 8:6 Comment(3)
BTW applet tag is deprecated in HTML 5 and unsupported by Chrome at all. So, I'd recommend you to use tag <object> insteadEvangel
1) The applet element was deprecated in HTML 4.01, not 5. 2) Just downloaded Chrome 9.0.597.98 and pointed it at a small, sand-boxed pair of applets at my site that use the applet element (pscode.org/test/appletresource/applet.html). It worked just fine despite that the page declared no HTML version (naughty me). So Chrome sure does recognize the applet element (a). 3) Use the object element and immediately lose the Mozilla based browsers. It is a pity they have not yet abandoned the silly embed element and conceded to the accepted standard, but that is the way it is. ...Penman
...cont. .. as a result of the object/embed debacle, I would recommend using the 'deployJava.js' (chase the links from stackoverflow.com/tags/applet/info) script to deploy applets. It is not a perfect solution, but because Oracle encourages developers to hot-link to it, and supports it, it is one of the best deployment options for applets. (a) I was cheating in the fact that Java was already installed & I guess Chrome was clever enough to pick up the current install & use it. But that is not the point. Chrome does recognize the applet element when Java is installed.Penman

© 2022 - 2024 — McMap. All rights reserved.