Solved

Applet works locally but not thru the Web Server

Posted on 1997-04-28
19
269 Views
Last Modified: 2013-11-23
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>
0
Comment
Question by:Doron
  • 10
  • 3
  • 2
  • +4
19 Comments
 
LVL 1

Expert Comment

by:roy020697
ID: 1219916
Is there a firewall? Most firewalls can be configured to exclude java applets; most system administrators are still paranoid about java WRT security.
0
 

Author Comment

by:Doron
ID: 1219917
Edited text of question
0
 

Author Comment

by:Doron
ID: 1219918
Edited text of question
0
 
LVL 2

Expert Comment

by:majkl
ID: 1219919
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.
 
0
 

Author Comment

by:Doron
ID: 1219920
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
0
 

Author Comment

by:Doron
ID: 1219921
Edited text of question
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219922
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.).
0
 
LVL 2

Expert Comment

by:webster030697
ID: 1219923
Could be a security violation. Are you trying to access any files? This would explain why it works locally, but not through the server.
0
 

Expert Comment

by:pedxing
ID: 1219924
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Doron
ID: 1219925
Edited text of question
0
 

Author Comment

by:Doron
ID: 1219926
Edited text of question
0
 

Author Comment

by:Doron
ID: 1219927
Edited text of question
0
 

Author Comment

by:Doron
ID: 1219928
Edited text of question
0
 

Author Comment

by:Doron
ID: 1219929
* 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

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1219930
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.
0
 

Expert Comment

by:lgn
ID: 1219931
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
0
 

Expert Comment

by:pedxing
ID: 1219932
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.
0
 

Author Comment

by:Doron
ID: 1219933
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…

0
 

Accepted Solution

by:
pedxing earned 200 total points
ID: 1219934
Ok, since you are offering...  I'll just repeat my previous answer for the benefit of those scanning previous solutions to questions.  I'll also point out the simpler answer proposed by lgn of changing the private variable to public.  This doesn't avoid the scoping issue altogether, but seems to work anyway.

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.

You should consider reporting this issue to Sun, so they can at least fix the problem of consistency in appletviewer, and possibly clarify this scoping in the standard.  If you just change the declaration of iShowButton, make sure you test your code on all possible platforms, too, since this scoping issue may still bite you on some platforms.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now