Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Jar and anonymous classes

6 views
Skip to first unread message

doug_van_horn

unread,
Dec 22, 1997, 3:00:00 AM12/22/97
to

Hi.

I've been fiddling with the jar utility and ran across this problem. I was
wondering if I'm missing something or if this has happened to anyone else:

Take the following class w/ an anonymous class:

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

public class Test extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString( (String)(new Object() {
public String toString() {return "The Hopper!";}
}), 10, 20);
}
}

It produces two .class files: Test.class and Test$1.class (in the default
package)

I run the following from the command line: jar -cf Test.jar *.class

and then I run appletviewer Test.html

Where the .html file is:
<APPLET CODE=Test.class ARCHIVE=Test.jar WIDTH=200 HEIGHT=200></APPLET>

I get the following error after attempting to run...

Exception occurred during event dispatching:
java.lang.IllegalAccessError: Test$1
at
at java.awt.Component.dispatchEventImpl(Component.java:1412)
at java.awt.Container.dispatchEventImpl(Container.java:837)
at java.awt.Component.dispatchEvent(Component.java:1393)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)

I believe this reproduces when I try to use a Local class as well. I haven't
tried a Method class yet...

Any insights or comments would be greatly appreciated!

Doug Van Horn
do...@wallst.com

doug_van_horn

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

Me again. Let me restate this, as I had an error in the previous post...

>>>File Test.java<<<
import java.applet.*;
import java.awt.*;

public class Test extends Applet {
public void init() {
}
public void paint(Graphics g) {

g.drawString(new Object() {
public String toString() { return "blah"; }
}.toString(), 10, 20);
}
}

>>>File Test.html<<<


<APPLET CODE=Test.class ARCHIVE=Test.jar WIDTH=200 HEIGHT=200>
</APPLET>

Create Test.jar as follows:
jar -cf Test.jar Test.class Test$1.class
(the two class files are created by a call to javac Test.java)

Then run appletviewer Test.html
and I get the following error:

C:\jdk115\mysrc>appletviewer Test.html


Exception occurred during event dispatching:
java.lang.IllegalAccessError: Test$1
at
at java.awt.Component.dispatchEventImpl(Component.java:1412)
at java.awt.Container.dispatchEventImpl(Container.java:837)
at java.awt.Component.dispatchEvent(Component.java:1393)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)

I am compiling and running with JDK 1.1.5

Now, if I compile this code and run it in the appletviewer _without_ the .jar
file, everything is fine. It's the .jar file that is screwing up. I'm also
curious about why the first 'at' line is blank. So my question is about .jar
files, really, and not inner classes.

I apologize for posting bug'd code. Just a dumb mistake. The above code *does*
compile and it *does* work outside a .jar file, just not when it's packaged up
in one...

Hope this clarifies things a bit...

Doug Van Horn
do...@wallst.com

Kevin Kelley

unread,
Dec 23, 1997, 3:00:00 AM12/23/97
to

Doug Van Horn wrote:

>Hi.
>
>I've been fiddling with the jar utility and ran across this problem. I was
>wondering if I'm missing something or if this has happened to anyone else:
>
>Take the following class w/ an anonymous class:
>

>import java.applet.*;
>import java.awt.*;
>
>public class Test extends Applet {
> public void init() {
> }
> public void paint(Graphics g) {

> g.drawString( (String)(new Object() {
> public String toString() {return "The Hopper!";}
> }), 10, 20);
> }
>}
>
>It produces two .class files: Test.class and Test$1.class (in the default
>package)
>
>I run the following from the command line: jar -cf Test.jar *.class
>
>and then I run appletviewer Test.html
>
>Where the .html file is:

><APPLET CODE=Test.class ARCHIVE=Test.jar WIDTH=200 HEIGHT=200></APPLET>
>

>I get the following error after attempting to run...


>
>Exception occurred during event dispatching:
>java.lang.IllegalAccessError: Test$1
> at
> at java.awt.Component.dispatchEventImpl(Component.java:1412)
> at java.awt.Container.dispatchEventImpl(Container.java:837)
> at java.awt.Component.dispatchEvent(Component.java:1393)
> at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)
>

>I believe this reproduces when I try to use a Local class as well. I haven't
>tried a Method class yet...
>


Interesting. I followed your example, using the jar utility and all,
and got:

C:\java\home>appletviewer test.html
Exception occurred during event handling:
java.lang.ClassCastException:
at Test.paint(Test.java:9)
at java.awt.Component.dispatchEventImpl(Component.java:1380)
at java.awt.Container.dispatchEventImpl(Container.java:818)
at java.awt.Component.dispatchEvent(Component.java:1361)
at
java.awt.EventDispatchThread.run(EventDispatchThread.java:58)

C:\java\home>java -version
java version "1.1_Final"


...so apparently it doesn't like the cast. I modified the example
like so:

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

public class Test extends Applet {
public void init() {
}
public void paint(Graphics g) {

g.drawString( (new Object() {
public String toString() {return "The Hopper!";}
}).toString(), 10, 20);
}
}


...and it works. So, your anonymous object is not a String,
and casting it to one is not appropriate. So then I tried
this:

public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println((String) test);
}
public String toString() { return "test!"; }
}

and got a compiler error: Invalid cast from Test to String.

All of which seems to make sense, to me. Doesn't seem to have
anything to do with the jar utility, just a bad cast.


Kevin Kelley
--
Starlight Software Co. - Java Cannery: auto-package your projects!
kel...@iguana.ruralnet.net http://www.ruralnet.net/~kelley/index.html

0 new messages