Search code examples
javaappletclassnotfoundexception

How to run a Java applet in the browser: "Class Not Found exception"


I'm trying to get a Java applet to display in the browser - I know this question has been asked a number of times but I can't seem to find the answer that works specifically for this case - over the past few days I've tried everything from moving the HTML file to various places in the directory structure to using <applet> vs. the deployJava() API.

The code runs fine as a standalone applet in Eclipse, but when I try to run it in the browser I get either a "ClassNotFound" or "ClassDefNotFound" exception. I've packaged the code into a .jar and placed the .jar within the same folder as the HTML file, with my java code as follows:

package myPackage;
import java.awt.*;
import java.applet.*;

public class myClass extends java.applet.Applet{
    public void init(){
        String latLong = getParameter("unUsedParameter");
    }

    public void paint(Graphics g){
        g.drawString("Hello World",50,25);
    }
}

and the Javascript code is as follows:

<script src="https://www.java.com/js/deployJava.js"></script>
<section id = "java">
    <script type="text/javascript">
        var attributes = {
            code:'myClass.class',
            archive: 'myApplet.jar',
            width:500, height:500
        };
        var parameters = {latLong: total_path}; 
        var version = '1.8'; 
        deployJava.runApplet(attributes, parameters, version);
    </script> 
</section>

I also tried using codebase: 'myApplet.jar' instead of archive: but that didn't work either - I keep getting one of the same two exceptions. HELP!

EDIT: First off, the code: attribute was incorrect in my original post, it should have read 'myClass.class' (this is corrected above). The answer that got it working was changing the code: attribute to code: 'myApplet/myClass' - thanks for your help!


Solution

  • Change your 'code' parameter to 'myPackage.myClass', instead of 'myApplet.class'.

    You have more insights about the declaration in this post, which I've shown a way that works Angular.js and Java Applet

    ie:

    <script>
        <!-- applet id can be used to get a reference to the applet object -->
        var attributes = { id:'cdigApplet', code:'cdig.CDigApplet', archive:'cdig-applet-1.0.jar', width:1, height:1, classloader_cache:'false'} ;
        var parameters = {persistState: false, cache_option:'no' } ;
        deployJava.runApplet(attributes, parameters, '1.8');
    </script>
    

    My Applet

    package cdig;
    
    import java.applet.Applet;
    import java.security.AccessController;
    import java.security.PrivilegedAction;
    import java.util.Base64;
    
    
    /**
     *
     * @author Ulysses Marins 
     */
    public class CDigApplet extends Applet
    {
    
        private static final long serialVersionUID = 1L;
    
        String ret;
    
        CDigApplet applet = this;
    
        @SuppressWarnings({ "rawtypes", "unchecked" })
        public String signFile(String fileID, String pin, String token)
        {
            AccessController.doPrivileged(new PrivilegedAction()
            {
                @Override
                public Object run()
                {
                    try
                    {
                        File objFile = new File(token);
    
                        System.out.println("Iniciando processo de assinatura.");
    
                        objFile.sign("json", sig);
    
                            System.out.println(ret);
                        } else {
                            throw new IllegalArgumentException("Não foi possível iniciar processo de assinatura.");
                        }
    
                    }
                    catch (Exception e)
                    {
                        String sl = "{\"success\":false," + "\"message\":\"" + e.getMessage() + "\"}";
                        ret = sl;
                        System.out.println(sl);
                    }
    
                    return null;
                }
            });
    
            return ret;
        }
    
        public void init(){
        }
    
        public void destroy(){
        }
    
    }