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 .....
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 .....
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
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.
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.
If it does not work, post your tag here
© 2022 - 2024 — McMap. All rights reserved.