Clojure Applets: Tutorial

120 views
Skip to first unread message

andi.x...@googlemail.com

unread,
Oct 13, 2009, 5:15:03 AM10/13/09
to Clojure
Hi,

since I found only questions about applets written in Clojure but not
really answers, I decided to write a small tutorial in the Clojure
wiki: http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Creating_an_Applet
Applet demo: http://www.xenoage.com/extern/clojurebook/applet/cljapp.html

The good news: It works :-)
The bad news: It took me some time to find our why I got a
SecurityException although the applet was signed. By using type hints
(#^Graphics2D) I could solve the problem. Perhaps this is a JVM bug
(related to http://bugs.sun.com/view_bug.do?bug_id=6595618 or so?),
since I should have all rights when the applet is signed. As a
workaround, I guess, if reflection can be avoided there should be no
problems when using Clojure within applets.

Bye,


Andi

Jon

unread,
Oct 13, 2009, 9:00:19 AM10/13/09
to Clojure
Hi,

On Oct 13, 11:15 am, "andi.xeno...@googlemail.com"
<andi.xeno...@googlemail.com> wrote:
> Hi,
>
> since I found only questions about applets written in Clojure but not
> really answers, I decided to write a small tutorial in the Clojure
> wiki:http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Creating_an...
> Applet demo:http://www.xenoage.com/extern/clojurebook/applet/cljapp.html
>
> The good news: It works :-)

Except, it doesn't seem to work on my Mac. In Safari (4.0.3) I get
"Applet test.Applet notinited" in the status bar. I'm using OSX
10.5.7.

/Jon

> The bad news: It took me some time to find our why I got a
> SecurityException although the applet was signed. By using type hints
> (#^Graphics2D) I could solve the problem. Perhaps this is a JVM bug
> (related tohttp://bugs.sun.com/view_bug.do?bug_id=6595618or so?),

andi.x...@googlemail.com

unread,
Oct 13, 2009, 9:35:40 AM10/13/09
to Clojure
I could test only on Windows, Linux and Solaris (no problems on these
systems).

Unfortunately, I have no Mac where I could test it. Perhaps a Mac user
can identify the problem?

DavidF

unread,
Oct 13, 2009, 10:22:35 AM10/13/09
to Clojure
It does work in Mac OS X 10.6.1 with Safari 4.0.3. I see a bordered
yellow box with "Hello World!" I'll try it later when I'm near a 10.5
box and see if I can figure out the problem.

On Oct 13, 8:35 am, "andi.xeno...@googlemail.com"

andi.x...@googlemail.com

unread,
Oct 13, 2009, 10:46:39 AM10/13/09
to Clojure
Perhaps Java 5 was running. I forgot to compile also for Java 5 (now
updated in tutorial and in demo).Could you please try again?
So far I know from two MacOS X 10.6 where it works and two 10.5 where
it fails.

pmf

unread,
Oct 13, 2009, 12:08:00 PM10/13/09
to Clojure
In your article, you mention the problematic size of 1.4MB of
clojure.jar. You might want to try clojure-slim.jar, which gets built
alongside clojure.jar, and is about 500KB.

Andreas Wenger

unread,
Oct 13, 2009, 2:36:38 PM10/13/09
to Clojure

> You might want to try clojure-slim.jar, which gets built
> alongside clojure.jar, and is about 500KB.

Good idea, thanks. I'll add it.

Another idea:

Since the applet itself needs not to be signed, it would be great if
there is an "official" precompiled clojure.jar which is certified from
a certificate authority. Then (as far as I know) the user is not
confronted with a daunting warning dialog.

Jon

unread,
Oct 14, 2009, 2:42:55 AM10/14/09
to Clojure
Yes, now it works on my Mac. Good! ;-)

/Jon

On Oct 13, 4:46 pm, "andi.xeno...@googlemail.com"

rob

unread,
Oct 14, 2009, 7:36:04 AM10/14/09
to Clojure
This is great, thanks for posting this! I did something just for my
own testing recently, and I have something more useful that is half-
completed, but here is a proof-of-principle clojure app I made. You
may want to take a look at this as it does not require writing any
Java, it is pure Clojure. This compiles to a class file, which you
then use. I had the same security issues as you though, so I'm going
to look over what you did to get around that (type declarations you
say?).

The compile script:

#!/bin/sh -e
base=/home/rob/bin/authored/games/clojure
cd $base
echo "(compile '$1)" | java -cp /home/rob/bin/clojure/clojure.jar:
$base/src:$base/classes: -Dclojure.compile.path=$base/classes
clojure.lang.Repl

The clojure code kitty.clj:

;; Clojure applet proof of principle
;; Author: Robert P. Levy

(ns kitty.kitty
(:import
(java.awt Graphics Frame Color Image Toolkit)
(java.net URL))
(:gen-class
:extends java.applet.Applet))

(defn -paint [applet g]
(doto g
(.setColor (. Color black))
(.drawString "Hello Kitty!" 60 40)
(.setColor (. Color red))
(.drawOval 40 20 120 30)
(.drawImage (. (. Toolkit getDefaultToolkit) getImage (new URL
"http://i-love-cartoons.com/snags/clipart/Hello-Kitty/Hello-Kitty-
painting.jpg")) 0 60 applet)
))


The html file HelloKitty.html:

<HTML>
<Head>
<Title>Hello Kitty </Title>
<Body>

View Source: <a href="kitty.clj">kitty.clj</a>

<Applet Code="kitty/kitty.class"
Archive="clojure.jar"
width="500"
height="350">
</Applet>



</Body>
</HTML>




On Oct 13, 5:15 am, "andi.xeno...@googlemail.com"
<andi.xeno...@googlemail.com> wrote:
> Hi,
>
> since I found only questions about applets written in Clojure but not
> really answers, I decided to write a small tutorial in the Clojure
> wiki:http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Creating_an...
> Applet demo:http://www.xenoage.com/extern/clojurebook/applet/cljapp.html
>
> The good news: It works :-)
> The bad news: It took me some time to find our why I got a
> SecurityException although the applet was signed. By using type hints
> (#^Graphics2D) I could solve the problem. Perhaps this is a JVM bug
> (related tohttp://bugs.sun.com/view_bug.do?bug_id=6595618or so?),

Andreas Wenger

unread,
Oct 15, 2009, 5:36:21 AM10/15/09
to Clojure
Fantastic news:

- Clojure applets do not have to be signed, when reflection can be
avoided (by type hints and so on)
- Java code is indeed not needed (thanks @ rob)

I've updated the tutorial accordingly.
Here is another Clojure applet which works without Java code and
without signing: http://chouser.n01se.net/misc/tree.html

Andreas Wenger

unread,
Oct 15, 2009, 6:24:33 AM10/15/09
to Clojure
However, compatibility with Java 5 is lost again.
Java 5 plugin shows the following error message when loading the
applet (even when Clojure compiler was started with Java 5, but I
guess, this does not matter anyway):

Java Plug-in 1.5.0_19
Verwendung der JRE-Version 1.5.0_19 Java HotSpot(TM) Client VM
Home-Verzeichnis des Benutzers = /home/andi

[...]

java.lang.VerifyError: class applet overrides final method
“Ÿʐh ‘ .ó¬ʐh ‘ (<- yeah, really.)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:
124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:172)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:144)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:687)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:723)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1813)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:652)
at sun.applet.AppletPanel.run(AppletPanel.java:326)
at java.lang.Thread.run(Thread.java:595)
/usr/share/themes/Human/gtk-2.0/gtkrc:51: error: lexical error or
unexpected token, expected valid token


Any idea?

Jon

unread,
Oct 15, 2009, 6:26:48 AM10/15/09
to Clojure
Hi,

I get "Applet net.n01se.Tree notinited" in the status bar on my Mac.
Did you compile for Java 5?

/Jon

On Oct 15, 11:36 am, Andreas Wenger <andi.xeno...@googlemail.com>
wrote:

Andreas Wenger

unread,
Oct 15, 2009, 7:50:32 AM10/15/09
to Clojure
Hi Jon,

"Applet net.n01se.Tree" ist just an example I found on the web. My new
demo code can be found here:
http://www.xenoage.com/extern/clojurebook/applet2/cljapp.html
But as already said, Java 5 doesn't like it (see error message two
posts above) and tells me that the bytecode generated by the Clojure
compiler was wrong... Hm...

indy

unread,
Oct 15, 2009, 8:16:58 AM10/15/09
to Clojure
The applet seems to work fine on my PowerPC Mac (10.4.11) with Java
1.5.0_19

On Oct 15, 12:50 pm, Andreas Wenger <andi.xeno...@googlemail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages