0

I'm trying to launch an applet through the command appletviewer filename.html which contains the code to launch an applet with specific width and height. When I launch the command I get the following error:

java.lang.ClassNotFoundException: ”dnaapplet.class”
    at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:219)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:633)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
    at sun.applet.AppletPanel.run(AppletPanel.java:380)
    at java.lang.Thread.run(Thread.java:745)

I control other posts about setting the classpath through java -cp, but I couldn't find out a solution.

The code of the html file is the following:

<applet 
code=”dnaapplet.class” 
width="835" 
height="180" 

></applet

Here is the code of the applet:

    import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class main_class extends Applet {

    Label Lseq1=new Label("Sequenza 1:");
    Label Lseq2=new Label("Sequenza 2:");
    TextField textSeq1=new TextField(30);
    TextField textSeq2=new TextField(30);
    TextField field_diff=new TextField(10);
    TextField field_perc=new TextField(10);
    Button compute=new Button("Confronta");
    Button reset=new Button("Resetta");
    Label error_label=new Label("Messaggi:");
    TextField msg=new TextField(33);


    public void init() {

        //aggiunta degli oggetti all'applet
        add(Lseq1); 
        add(textSeq1);
        add(Lseq2);
        add(textSeq2);
        add(new Label("Differenze:"));
        add(field_diff);
        add(new Label("Uguali al:"));
        add(field_perc);
        add(compute);
        add(reset);
        compute.addActionListener(seqInserted);
        reset.addActionListener(click);
        add(error_label);
        add(msg);
        msg.setText("Inserire due sequenze di uguale lunghezza.");

    }

    ActionListener seqInserted=new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            //getta il testo dei due campi di testo
            String seq1=textSeq1.getText();
            String seq2=textSeq2.getText();
            int length1=seq1.length();
            int length2=seq2.length();

            /*il primo if verifica se le sequenze sono lunghe uguali, il secondo
            verifica se tutti i caratteri inseriti sono validi: se vengono superati 
            entrambi i controlli, si passa alla computazione delle differenze*/

            if (length1==length2) {

                //if che controlla se i caratteri inseriti sono validi e chiama
                //le relative funzioni
                if ((main_class.checkInput(seq1, seq2, length1))==1) {

                    //chiamata delle funzioni di svolgimento dei calcoli
                    int diff=main_class.confrontoSequenze(seq1, seq2, length1);
                    field_diff.setText(""+diff);
                    field_perc.setText(""+main_class.percentuale(diff, length1)+"%");

                //almeno un carattere non è valido
                } else {
                    msg.setText("Carattere inserito non valido");
                    textSeq1.setText("");
                    textSeq2.setText("");
                    field_diff.setText("");
                    field_perc.setText("");
                }

            //questo else si apre quando le stringhe sono di lunghezza diversa
            } else {
                textSeq1.setText("");
                textSeq2.setText("");
                field_diff.setText("");
                field_perc.setText("");
                msg.setText("Errore:lunghezza sequenze diversa.");
                }
            }   
        };  

    //gestore del click del reset button
    ActionListener click=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            textSeq1.setText("");
            textSeq2.setText("");
            field_diff.setText("");
            field_perc.setText("");
            msg.setText("");
            }       
    };

}


    /*funzione che restituisce 0 almeno un carattere non è valido,
    1 se tutti i caratteri sono validi*/

    public static int checkInput(String seq1,String seq2, int length ) {

        int check1=1;
        int check2=1;
        int total_check=1;

        for (int i=0; i<=length-1; i++) {
            if (seq1.charAt(i)!='a' && seq1.charAt(i)!='A' && seq1.charAt(i)!='c' && seq1.charAt(i)!='C' && seq1.charAt(i)!='g' && seq1.charAt(i)!='G' && seq1.charAt(i)!='t' && seq1.charAt(i)!='T')
                check1=0;
        }


        for (int i=0; i<=length-1; i++) {
            if (seq2.charAt(i)!='a' && seq2.charAt(i)!='A' && seq2.charAt(i)!='c' && seq2.charAt(i)!='C' && seq2.charAt(i)!='g' && seq2.charAt(i)!='G' && seq2.charAt(i)!='t' && seq2.charAt(i)!='T')
                check2=0;
        }

        if (check1==0 || check2==0)
            total_check=0;

        return total_check;

    }


    public static int confrontoSequenze (String seq1, String seq2,int lenght) {

        int eq=0;
        int diff=0;

        for (int i=0; i<=lenght-1; i++) {

            //controlla se le stringhe sono uguali
            if (seq1.charAt(i)==seq2.charAt(i)) 
                eq++;
            else {
                diff++;
            }
        }
        return diff;
    }

    public static float percentuale(int diff, int lenght) {

        float perc;
        int eq=lenght-diff;

        perc=(eq*100)/lenght;
        return perc;
    }
}

This is my first applet so maybe my code is not perfect. The comments and strings are in italian, I'm sorry. The applet extends a program I made in Java last year and compares two DNA sequences and returns how much they're similar.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sentientmachine
  • 347
  • 3
  • 14
  • 1
    please post your code – Joe Taras Mar 04 '15 at 14:34
  • In wich directory is your class file located. It must be in the same folder as your html file. – Jens Mar 04 '15 at 14:37
  • I don't think the code is the problem, if I run it from Eclipse it works perfectly. – sentientmachine Mar 04 '15 at 14:48
  • @CrazyUser12 Your class is named `main_class`, why do you think that `code=”dnaapplet.class”` will work? It should be `code="main_class.class"`. And `main_class.class` should be in the same directory as the html file – BackSlash Mar 04 '15 at 14:57
  • Doesn't work even if renamed. Same error. And file .class and .html file are in the same directory – sentientmachine Mar 04 '15 at 15:02
  • @BackSlash *"It should be `code="main_class.class"`"* It is actually supposed to be the 'fully qualified name' of the class, which does not include the file type (`.class`). – Andrew Thompson Mar 05 '15 at 02:07
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Mar 05 '15 at 02:07

0 Answers0