Link to home
Start Free TrialLog in
Avatar of Doron
Doron

asked on

Applet works locally but not thru the Web Server

Hi,

We have a very small Java Applet (9 statements, single file, code follows) that works fine locally (appletviewer Gui05.html), but when run thru a Web Server (appletviewer http://.../Gui05.html) on the same machine, it generates the following error message:

Exception occurred during event dispatching:
java.lang.IllegalAccessError: Gui05.iShowButton
        at Gui05$ListItemSelectedListener.itemStateChanged(Gui05.java:23)
        at java.awt.List.processItemEvent(List.java:652)
        at java.awt.List.processEvent(List.java:630)
        at java.awt.Component.dispatchEventImpl(Component.java:1442)
        at java.awt.Component.dispatchEvent(Component.java:1382)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)

The compilation is with jdk1.1.1
The appletviewer is Sun's current appletviewer (comes with jdk1.1.1), same results with Microsoft's appletviewer (beta).
Tried 2 WebServers: O'Reilly's WebSite 1.1e, Some old Netscape WebServer (beta).
Tried both Windows NT 4.0 and Windows 95.
There is no firewall we are trying to go through.

Any comment is welcome.
Thanks,

Doron


THE SCENARIO:
You must have a Web Server running on your machine in order to encounter the problem.
Put the following two files in the same directory with the file names Gui05.java, Gui05.html respectively.
Compile the Gui05.java file with jdk1.1.1
Run the Applet with an appletviewer that supports jdk1.1.1 through
the Web Server (appletviewer http://.../Gui05.html)
Click with the left mouse button on the 'Click me' item in the List.
The line "List item selected" followed by the error message should appear.
Make sure when running the Applet that the CLASSPATH doesn't include the directory in which the Gui05.class is. (If it does the Class Loader will take the class directly rather than through the Web Server.)
Notice that when running not through the Web Server (appletviewer Gui05.html) the Applet works fine.
Please let me know, whether you get the error message or not, of the results and your environment, so if it's a bug in one of the tools we will be able to identify it.

THE APPLET'S CODE: (file name: Gui05.java)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Gui05 extends Applet
{
    private Button      iShowButton = new Button("Show");

    public void init()
    {
            iShowButton.setEnabled(false);
            List list = new List(5, false);
            list.addItem("Click me");
            list.addItemListener(new         ListItemSelectedListener());
            add(list);
            add(iShowButton);
    }

    class ListItemSelectedListener implements ItemListener
    {
            public void itemStateChanged(ItemEvent e)
            {
                                System.out.println("List item selected");
                                iShowButton.setEnabled(true);
            }
    }
}

A SAMPLE HTML FILE: (file name: Gui05.html)

<html>
<head>
<title>Gui05</title>
</head>
<body>
<hr>
<applet
    code="Gui05.class"
    id=Gui05
    width=320
    height=260 >
</applet>
<hr>
<a href="Gui05.java">The source.</a>
</body>
</html>
Avatar of roy020697
roy020697

Is there a firewall? Most firewalls can be configured to exclude java applets; most system administrators are still paranoid about java WRT security.
Avatar of Doron

ASKER

Edited text of question
Avatar of Doron

ASKER

Edited text of question
Are you viewing applet with browser capable to view applets from JDK 1.1.1. If not, then thaths why they don't run. I think browsers with JDK 1.1.1 are still in beta phase.
 
Avatar of Doron

ASKER

To roy: Nice idee but the applet doesn't go through any firewall. Thanks

To majkl: To the best of my knowledge Sun's current appletviewer is capabale of running jdk1.1.1 code. Thanks
Avatar of Doron

ASKER

Edited text of question
Your code is requesting a stack dump as part of a try catch block as a result of procesing the event generate by the button you specified and is paerantly asosiated with a list box in the dialog (do you have one defined?).

In order to properly diagnose, can you post how you defined the action associated to button, the list box (if there is one), the code executed by the button and any other pertinant info (does the applete implement runnable, declarations of variables used in the afected section, etc.).
Could be a security violation. Are you trying to access any files? This would explain why it works locally, but not through the server.
The error java.lang.IllegalAccessError indicates that an attempt to instantiate a new object for a class failed (it is thrown by Class.newInstance()).  It looks to me that there is a class being dynamically loaded that is accessible from the local machine but does not have the proper permissions or is not accessible at all from the Web Server.  From the error message, I would guess that it is panels.SingleListPanelPlus.iCommandEventSupport.

Is that object either being created or used for the first time at line 62 of SingleListPanelPlus.java?  I say "used" because you can get class loading delayed until necessary.

Check whether the .class file is really visible to the web server.  See whether all file permissions are set so that you can access the object.  Try writing an applet which just creates an iCommandEventSupport object, and then calls one of its functions so you can be sure it is loaded.  Make sure the server is set up to allow downloading from that particular directory.

If you are able to get at iCommandEventSupport on the web server using the simplified applet then the illegal access is caused by something else.  What that something else could be, you might be better able answer than I am.  But hopefully understanding the behaviour you are seeing will help you figure out what the issue is.

Good luck.  Add comments of you discover more information that would be useful in analyzing the problem, or if you'd like more help on the problem.
Avatar of Doron

ASKER

Edited text of question
Avatar of Doron

ASKER

Edited text of question
Avatar of Doron

ASKER

Edited text of question
Avatar of Doron

ASKER

Edited text of question
Avatar of Doron

ASKER

* Due to the comments I got I have isolated and simplified the problem to a very small Applet that is now attached to the question. The question editor is not very friendly so the Applet’s indentation is somewhat non-existent.

To pedxing & jpk: Thanks for your advice to post a simplified problem.

To pedxing: Class.newInstance() throws IllegalAccessException rather than IllegalAccessError.
I think IllegalAccessError is usually generated when the class being loaded fails a verification test
From Sun’s documention about IllegalAccessError: extends IncompatibleClassChangeError. Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
As you will be able to see with the code attached, it seems that the first statement in the class in which the Error occurs is executed before the Error is generated.

To jpk: No try catch block, this is an Error, not an Exception.

To webster: Would it be a security violation I would expect a security Exception rather than this error. Thanks

The following buf is documented in Suns Java Site:

4032466Item events are not properly generated when the state of a Checkbox or List is changed programmatically.

This could be the sourc3e of the error.

Workarround:

use EventListener for list boxes and Check boxes.
Did you try to declare your iShowButton as a public variable instead of a private one ? May be this is the reason why your inner class generates an Illegal Access error. If this is the case, I can't explain why you get a different behavior between locally and web server loaded classes
You are right about IllegalAccessError vs. IllegalAccessException.  My mistake.

Based on the code you posted, this looks like some sort of incompatibility (ie. bug) in scoping rules.  Ignoring the no-no of using System in an applet for the moment, have you considered rewriting the program like this:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Gui05 extends Applet
{
  private Button iShowButton = new Button("Show");
  public void init()
  {
    iShowButton.setEnabled(false);
    List list = new List(5, false);
    ListItemSelectedListener listener = new ListItemSelectedListener();
    listener.setLocalButton(iShowButton);
    list.addItem("Click me");
    list.addItemListener(listener);
    add(list);
    add(iShowButton);
  }

  class ListItemSelectedListener implements ItemListener
  {
    private Button iLocalButton;
    public void itemStateChanged(ItemEvent e)
    {
//    Don't need next line. If IllegalAccessError, you got here.
//    System.out.println("List item selected");
//    Should verify that iLocalButton is not null in real code
      iLocalButton.setEnabled(true);
    }
    public void setLocalButton(Button mybutton)
    {
      iLocalButton = mybutton;
    }
  }
}

I haven't tested this code, so if I've forgotten anything or if it doesn't work, sorry about that.  I don't have JDK1.1.1 installed right now, or I'd give your code a try.

You should be able to see what I was getting at, anyway.  Avoid the issue of what the scope of iShowButton is and see if the problem goes away.  This may not really be a solution for your larger program, but running this test may help establish that it is a scoping issue.

BTW, I'm not offering an answer but a comment, since I believe you are only allowed so many rejections before the question is dropped (5?), and I think you are close.

I hope this is of some help.  Good luck.
Avatar of Doron

ASKER

As lgn offered, I changed the modifier of iShowButton, this solves the problem. (It works when protected, public or the default modifier.).
Thanks for the good workaround !!!
It’s probably somewhere there but I couldn’t find in Sun’s documentation about inner classes whether a private variable of the top class should be visible to an inner class, I guess it should.
Why different behaviors between locally and web server loaded classes I don’t know.

To jpk: I think that ‘changed programmatically’ (let’s say for a List) means that a property of the List was changed by the program (E.g. List.select()) and not by the user (E.g. mouse click). If so, then this is not the case of a 'changed programmatically'.
I was not aware of this bug and thank you for telling me about it.
About EventListener, I don’t think I know what you mean, ItemListener extends EventListener, so please be more specific.

To pedxing: I did actually try your Applet and it solves the problem.

Though the problem is still open in the sense that we don’t know the reason (where is the bug), there are workarounds now.
As for the points, everyone have been helpful, seems that pedxing made a big effort, so if you’re interested…

ASKER CERTIFIED SOLUTION
Avatar of pedxing
pedxing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial