Applet does not run in browser

Asked

Viewed 286 times

4

Do not run Applet. Just give this return.

inserir a descrição da imagem aqui

This is my java class.

public class SiteSelector extends JApplet {

private HashMap<String, URL> sites; // nomes e URLs de site
private ArrayList<String> siteNames; // nomes de sites
private JList siteChooser; // lista do sites a escolher

//le os parametros e configura a GUI
public void init(){
    sites = new HashMap<String,URL>();
    siteNames = new ArrayList<String>();

    //otem os parametros do documento XHTML
    getSitesFormHTMLParmeters();

    //cria componentes GUI e a interface layout
    add( new JLabel("Choose a stie to browser"), BorderLayout.NORTH);

    siteChooser = new JList(siteNames.toArray()); // preenche a JList
    siteChooser.addListSelectionListener(new ListSelectionListener() {
        //vai ao site selecionado pelo usuário
        @Override
        public void valueChanged(ListSelectionEvent arg0) {
            // TODO Auto-generated method stub

            //obtem o nome do site selecionado
            Object object = siteChooser.getSelectedValue();

            //utiliza o nome do site para localizar a URL correspondente
            URL newDocument = sites.get(object);

            //obtem o conteiner de applets 
            AppletContext browser = getAppletContext();

            //instrui o conteiner de applets a mudar as paginas
            browser.showDocument(newDocument);
        }
    }); // classe interna anonima

    add(new JScrollPane(siteChooser),BorderLayout.CENTER);
}

//obetem os paramentros do documento XHTML
private void getSitesFormHTMLParmeters(){
    String title; // titutlo do site
    String location = ""; // localização do site
    URL url; // URL da localização
    int counter = 0; // conta o nuemro de sites

    title = getParameter("title" + counter); // obtem o primeiro titulo do site

    //faz um loop até que não haja mais parametros no documento XHTML
    while(title != null){
        //obtem a localização do site
        location = getParameter("location" + location);

        try{//coloca titulo/URL no HashMap e titulo na ArrayList
            url = new URL(location); // converte a localização em URL
            sites.put(title,url); // coloca titulo/URL no HashMap
            siteNames.add(title); // coloca o titulo no ArrayList
        }catch(MalformedURLException urlException){
            urlException.printStackTrace();
        }
        ++counter;
        title = getParameter("title" + counter); // obtem o proximo titulo do site
    }
}

}

This is my HTML.

inserir a descrição da imagem aqui

What am I doing wrong? The html file I put in the /bin directory merged with . class

  • How are you opening this HTML file? Are you using a local server? Simply opening the HTML file in your browser? Your window reminds me a lot of the appletrunner (or apppletviewer, I don’t know - I haven’t touched Applets for years), you would be by chance using a command like this to try to open your applet?

  • I am opening in the browser normally.

  • Type, file:///caminho/pro/arquivo.html?

  • 1

    That’s right. And it’s an example of a Deitel java book.

  • 1

    As much as I look, I can’t find the information whether or not to run an applet on the local file system, no server... I’m going to let someone with recent Java experience answer, so unfortunately I don’t really remember that detail.

  • Tranquil mgibsonbr. I thank you for your attention.

  • The browser comes to ask you the execution permission questions for this applet?

  • Yes enough. I give the permission. And does not execute the applet. I already downloaded security and still can not run.

  • 1

    Try to put in the java settings the path to the file as safe or allowed website applet execution

  • Did you find a solution? Poste as an answer to help other people.

Show 5 more comments

1 answer

3

Assuming you haven’t found the problem yet, I notice that in your HTML you have some errors that cause an incorrect or null visualization of your applet.

Tag applet

Where you define height, you have a misspelling where heiht should be height:

<applet codebase="." code="solo.SiteSelector.class" width="300" heiht="75">
  <!-- ... -->
</applet>

Should be:

<applet codebase="." code="solo.SiteSelector.class" width="300" height="75">
  <!-- ... -->
</applet>

This could lead to your applet zero height, where although running, you end up not seeing it.

Note:
As can be seen in documentation (English), this is a mandatory attribute, which by itself, the typo is sufficient to justify your problem.

Attribute code

In your attribute code on the tag applet you have written:

code="solo.SiteSelector.class"

But I don’t know where the solo, judging by the code in your question you should just have:

code="SiteSelector.class"

You can see in the documentation (English) that the attribute code is supposed to contain the class name, and your class is called SiteSelector.

Note:
On the other hand, you can also pass packagename.classname.class, that is to say, meuPacote.minhaClasse.class as can be seen in documentation on this page (English).
In your case it will be package solo and the class SiteSelector if that is the reality.

Attribute codebase

The attribute codebase is used to specify the path to the files. It is used in conjunction with the attribute value code.

In your case you are indicating that the file is in:

./solo.SiteSelector.class

The . followed by solo.SiteSelector.class should give a valid path, confirms if it is your case.

Tag param

On your second tag <param/> you have the attribute containing the value with the misspelled name:

<param name="location0" valeu="http://java.sun.com/">

Should be:

<param name="location0" value="http://java.sun.com/">

After solving the above you may have solved the problem or you may have exceeded part of the problem.

Here are some ideas to better explore the situation if you’ve only overcome part of the problem:

  1. Change tab background color <body/> to know if Applet is running or not, because the background should turn green and your Applet will have a different color background, probably white:

    <body bgcolor="green">
    
  2. Usually the browsers are 32bit, but you already have 64bit versions available, check if you are using the Java version 32 bit or 64bit and cross-check the information with the browser to see if both share the same architecture.

  3. Try your Applet with the Appletviewer (English) you can run from the terminal:

    appletviewer meuFicheiro.html
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.