http://www.java.com/en/download/help/redximage.xml
most or nearly all applets from other pages display well in browser
this applet works with the appletviewer.
I got no help on the IRC channel irc.freenode.net #java as they have
the policy no applets.
I type this in a file
import javax.swing.*;
public class PreviewApplet2 extends JApplet
{
static final long serialVersionUID = -3850477712741615045L;
public void init()
{
JLabel niceLabel = new JLabel("Java is fun!");
ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
niceLabel.setIcon(dukeIcon);
getContentPane().add(niceLabel);
}
}
and compiles to a class
javac -Xlint PreviewApplet2.java
I have the examples from here
http://www-cse.ucsd.edu/users/savitch/ # my book is not listed,
Java:An Introduction to Computer Science and Programming, 2nd Edition
some other examples runs fine, from the command line, on windows
in the appletviewer and in the browser.
But this one gets
Red X image displayed where the applet should be, applets won't run
I use appletviewer PreviewApplet2.html
and that works fine.
permissions for the image is
-rwxrwxrwx duke_waving.gif
a change in permissions to
-rw-r--r-- did not help.
on the Tools Java console I can se
Java Plug-in 1.6.0_05
Using JRE version 1.6.0_05 Java HotSpot(TM) Client VM
User home directory = /export/home/morten
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.security.AccessControlException: access denied
(java.io.FilePermission duke_waving.gif read)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:853)
at sun.awt.SunToolkit.getImage(SunToolkit.java:867)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at PreviewApplet2.init(PreviewApplet2.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
Java Plug-in 1.6.0_05
Using JRE version 1.6.0_05 Java HotSpot(TM) Client VM
User home directory = /export/home/
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.security.AccessControlException: access denied
(java.io.FilePermission duke_waving.gif read)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:853)
at sun.awt.SunToolkit.getImage(SunToolkit.java:867)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at PreviewApplet2.init(PreviewApplet2.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
java.security.AccessControlException: access denied
(java.io.FilePermission duke_waving.gif read)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:853)
at sun.awt.SunToolkit.getImage(SunToolkit.java:867)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at PreviewApplet2.init(PreviewApplet2.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
java.security.AccessControlException: access denied
(java.io.FilePermission duke_waving.gif read)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:853)
at sun.awt.SunToolkit.getImage(SunToolkit.java:867)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at PreviewApplet2.init(PreviewApplet2.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
java.security.AccessControlException: access denied
(java.io.FilePermission duke_waving.gif read)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at
java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:853)
at sun.awt.SunToolkit.getImage(SunToolkit.java:867)
at javax.swing.ImageIcon.<init>(ImageIcon.java:81)
at javax.swing.ImageIcon.<init>(ImageIcon.java:107)
at PreviewApplet2.init(PreviewApplet2.java:11)
at sun.applet.AppletPanel.run(AppletPanel.java:425)
at java.lang.Thread.run(Thread.java:619)
in the error console I can see the above msg.
I did google for
java.io.FilePermission duke_waving.gif
and found this explanation
How are you running the applet from the command line? In a browser? In
the appletviewer? Or as an application?
Security settings are different for all three. When applets run in a web
browser, access to the file system is, by default, not permitted. That
is normal.
End of explanation
I look it up from sun
http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#applet
Loading Images Into Applets
Applets generally load image data from the computer that served up
the applet.
For me it looks like a contradiction.
If I include this line of code in the java file
ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
niceLabel.setIcon(dukeIcon);
I do expect java to do so and not additionally
append an <IMG SRC="filewithpicture.gif"> to the HTML code.
best regards
Morten
>Error as described here,
See http://mindprod.com/jgloss/applet.html
for general hints on things to look for.
Turn on your console. An error message may be sitting there which
will give you a much better clues why it is failing.
http://mindprod.com/jgloss/console.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> Error as described here,
>
> http://www.java.com/en/download/help/redximage.xml
[...]
> java.security.AccessControlException: access denied
> (java.io.FilePermission duke_waving.gif read)
[...]
This is _good_! It means an applet can't load just any old file, but you
should be able to load it from your jar.
John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
[snip long stack trace]
>
> in the error console I can see the above msg.
>
> I did google for
> java.io.FilePermission duke_waving.gif
>
> and found this explanation
>
>
>
> How are you running the applet from the command line? In a browser? In
> the appletviewer? Or as an application?
>
> Security settings are different for all three. When applets run in a web
> browser, access to the file system is, by default, not permitted. That
> is normal.
>
>
> End of explanation
>
> I look it up from sun
>
> http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#applet
>
>
> Loading Images Into Applets
>
> Applets generally load image data from the computer that served up
> the applet.
>
>
> For me it looks like a contradiction.
No, it's not a contradiction. Applets require a web server, and they load images
from the same web server as they were themselves loaded from (or from within a
jar obtained from the same server). If you attempt to load an applet from the
local filesystem, as you have done here, then the JVM classloader can load the
class you specify, but your class file cannot access the local filesystem
unless you sign the applet. This is for everyone's protection. Otherwise an
applet which you accessed via some random URL would be able to access your
filesystem, which I'm sure you don't want to be allowed.
>
> If I include this line of code in the java file
>
> ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
> niceLabel.setIcon(dukeIcon);
>
> I do expect java to do so
but the applet plugin won't do so, and for a very good reason.
> and not additionally
> append an <IMG SRC="filewithpicture.gif"> to the HTML code.
Huh?
--
Nigel Wade
>>
>> End of explanation
>>
>> I look it up from sun
>>
>> http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#applet
>>
>>
>> Loading Images Into Applets
>>
>> Applets generally load image data from the computer that served up
>> the applet.
>>
>>
>> For me it looks like a contradiction.
>
> No, it's not a contradiction. Applets require a web server, and they load images
> from the same web server as they were themselves loaded from (or from within a
> jar obtained from the same server). If you attempt to load an applet from the
> local filesystem, as you have done here, then the JVM classloader can load the
> class you specify, but your class file cannot access the local filesystem
> unless you sign the applet. This is for everyone's protection. Otherwise an
> applet which you accessed via some random URL would be able to access your
> filesystem, which I'm sure you don't want to be allowed.
>
>> If I include this line of code in the java file
>>
>> ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
>> niceLabel.setIcon(dukeIcon);
>>
>> I do expect java to do so
>
> but the applet plugin won't do so, and for a very good reason.
>
Interesting </Morten>
>> and not additionally
>> append an <IMG SRC="filewithpicture.gif"> to the HTML code.
>
> Huh?
>
I will discuss the HTML <IMG SRC=""> tags later. </Morten>
Hi Nigel,
What! Applets require a web server?
Interesting.
this shows that some works and others woun't work.
esp this one http://tinyurl.com/35dt2b
it works like charm from the
Apache/2.0.61 (Unix) Server at xxxx Port 80
with images and permissions and no jar file
and this one http://tinyurl.com/2t856d
always fails with nasty stack trace.
And the Red X image displayed where the applet should be,
except when run from the appletviewer.
the example I'm using which is supposed to work both has as Java code
>> ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
>> niceLabel.setIcon(dukeIcon);
and in the html file containing the <applet> ... </applet> tag also
has an html tag like <IMG SRC="filewithpicture.gif">
In the docs IIRC I at least remember to have read something like
<applet> is incorrect it should have been <object> </object>
but since there are so many different browsers the safe and old
fashioned way is the traditional way and still IIRC if I remember
correctly the recommended tag is in 2008 the <applet>.
My interpretation of this is in order to be browser compatible
also as an additional safety tag also to include an <IMG> tag.
This can be due to the population of nonstandards proprietary web
browsers. I'm not discussing safari, opera or Linux web browsers.
but this is about a java applet and I feel it would be nearly correct
to create an html file containing only the <applet> </applet>
tag like this
<applet code=PreviewApplet2.class width="400" height="200">
</applet>
Applets require a web server is an incorrect statement.
file:///usr/jdk/instances/jdk1.6.0/demo/applets/TicTacToe/example1.html
works like charm
in my browser
in my applet viewer
and from my homepage
and it has pictures.
I think it must be a misunderstanding.
please take a close look here.
http://java.sun.com/docs/books/tutorial/deployment/jar/examples/TicTacToe.html
<title>TicTacToe</title>
<hr>
<applet code=TicTacToe.class width=120 height=120>
</applet>
<hr>
<a href="TicTacToe.java">The source.</a>
For me this looks like an implementation
without any signature on the jar file.
and the html <IMG> tag is not present, so I will keep it
out of my example code for the moment.
Currently I have not done the effort with signing jar files or creating
an applet as a jar file.
best regards
Morten
Ok, it looks like I was wrong.
I thought that an applet loaded from the filesystem had no access to the local
filesystem other than the original class/jar file it was loaded from. It
appears that the applet *can* access the local filesystem via the classloader -
it still has no direct access.
If you look at the source code in that applet you will see that it is loading
the images via the classloader:
notImage = getImage(getCodeBase(), "images/not.gif");
crossImage = getImage(getCodeBase(), "images/cross.gif");
In the applet of yours which fails you are attempting to access the file via the
filesystem directly (rather than via the classloader):
ImageIcon dukeIcon = new ImageIcon("duke_waving.gif");
this is not allowed and results in
java.security.AccessControlException: access denied
--
Nigel Wade
>
>What! Applets require a web server?
you can run them locally too. With a little work, you can also run
them as an Application.
see http://mindprod.com/jgloss/applet.html
for how
Thanks
it seems to work However I found this information easier to understand.
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html
http://faq.javaranch.com/java/AppletsFaq
How can an applet load an image?
Image img = getImage(getCodeBase(), "anImage.gif");
that was the right approach.
// "Java Tech"
// Code provided with book for educational purposes only.
// No warranty or guarantee implied.
// This code freely available. No copyright claimed.
// 2003
//
// Begun with StartJApplet2
// minor modifications by morton
/*
<applet code="ImageApplet.class" width="300" height="300">
</applet>
*/
import javax.swing.*;
import java.awt.*;
/** Demonstrate drawing an image. **/
public class ImageApplet extends JApplet
{
static final long serialVersionUID = -7028750069844823554L;
public void init ()
{
Container content_pane = getContentPane();
// Grab the image.
Image img = getImage (getCodeBase(), "duke_waving.gif");
// Create an instance of DrawingPanel
DrawingPanel drawing_panel = new DrawingPanel(img);
// Add the DrawingPanel to the contentPane.
content_pane.add (drawing_panel);
} // End init
} // End ImageApplet
/** Draw on a JPanel rather than on JApplet's Panel. **/
class DrawingPanel extends JPanel
{
static final long serialVersionUID = 5548184581010973132L;
Image img;
DrawingPanel(Image img)
{
this.img = img;
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
// Use the image width& width to find the starting point
int img_x = getSize().width/2 - img.getWidth(this)/2;
int img_y = getSize().height/2 - img.getHeight(this)/2;
//Draw image at centered in the middle of the panel
g.drawImage(img, img_x, img_y, this);
} // paintComponent
} // class DrawingPanel
/*
javac -Xlint ImageApplet.java
bash-3.00$ javac -Xlint ImageApplet.java
ImageApplet.java:19: warning: [serial] serializable class ImageApplet
has no definition of serialVersionUID
public class ImageApplet extends JApplet
^
ImageApplet.java:45: warning: [serial] serializable class DrawingPanel
has no definition of serialVersionUID
class DrawingPanel extends JPanel
^
2 warnings
bash-3.00$
bash-3.00$ serialver ImageApplet
ImageApplet: static final long serialVersionUID = -7028750069844823554L;
bash-3.00$ serialver DrawingPanel
DrawingPanel: static final long serialVersionUID = 5548184581010973132L;
bash-3.00$
*/
it has two classes and is reported to load on all tested systems with a
recent jvm.
best regards
Morton