Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

problem: security using IDE's appletviewer

Thread view: 
bH - 05 May 2008 14:49 GMT
Hi All,

Viewing an image, this applet, which compiles and shows an
image in the browser page IE with no error.

The image is located >outside< of the program folder.

I am having the problem with the IDE's appletviewer that
will not allow a image file to be read. The error
message on the AppletViewer's message bar at run time is:

exception:java.security.AccessControlException:Access
denied(java.io.FilePermission \C:\JBsm.jpg
read)

That said, I have looked at Canadian Mind Products
> P words > policy

The instructions  say to use:

grant {
permission java.security.AllPermission;
};

I have added these lines above but now there is an error at
compile time for this applet:
<identifier> expected (obviously after the word "grant")

Something is missing but I have no idea. I am using
jdk1.5.0_12 and jre1.5.0_12
and stored in that same folder is there jre1.6.0_03 and
jre1.6.0_05

Thanks in advance for your help.

Here is the applet code:

import java.awt.*;
import java.applet.Applet;
import java.awt.Image;

public class ImageApplet extends Applet
 {
 private Image ioStream;
 private String errorMessage = null;

 public void init() {
   try
     {
     //ioStream = getImage(getCodeBase(),
     // "image/JBsm.JPG" );
     //above line works with both the IE browser page
     // and applet viewer
     ioStream = getImage(getCodeBase(),
"file:/C:/JBsm.JPG" );
     //above line works with the Browser page but fails
     //using the applet viewer

     //Insure image is downloaded before showing it
     MediaTracker tracker = new MediaTracker( this );
     tracker.addImage( ioStream, 0 );
     tracker.waitForID( 0 );
     repaint();
     }
   catch (InterruptedException netProblem )
     {
     errorMessage = "Could not reach image";
     }
   }

 public void paint( Graphics display)
   {
   if ( errorMessage == null )
     display.drawImage( ioStream, 0, 0, this );
   else
     display.drawString( errorMessage, 10, 10 );
   }
 }

bH
Mark Space - 05 May 2008 17:26 GMT
> That said, I have looked at Canadian Mind Products
>  > P words > policy
[quoted text clipped - 8 lines]
>  compile time for this applet:
>  <identifier> expected (obviously after the word "grant")

Read the rest of Roedy's page.  Especially the part where he tells you
that the policy file is outside of your program.

<quote>
Where are the policy files?
Exactly how many policy files you have and where they are is controlled
by settings in the C:\Program
Files\java\jre1.6.0_06\lib\security\java.security or C:\Program
Files\Java Web Start\java.security. The Opera browser has its own policy
file at C:\Program Files\Opera\classes\Opera.policy.

The default is to have:

   1. a single system-wide policy file J:\Program
Files\java\jdk1.6.0_06\jre\lib\security\java.policy in the
java.home\lib\security directory.
   2. a user-specific policy file user.home/.java.policy, e.g.
"C:\Documents and Settings\%username%\.java.policy". In Vista, look in
"C:\Users\%username%\.java.policy".
</quote>

>       //ioStream = getImage(getCodeBase(),
>       // "image/JBsm.JPG" );
[quoted text clipped - 4 lines]
>       //above line works with the Browser page but fails
>       //using the applet viewer

Realistically, this isn't going to work.  No one but you is going to
edit their own policy files.  Make this image into a resource, and use
getResourceAsStream().  That's the correct way to package extra files
with an applet.
Knute Johnson - 05 May 2008 23:02 GMT
> Hi All,
>
[quoted text clipped - 76 lines]
>
> bH

If you are using the appletviewer or browser to read files from the
local disk, they must be in the same directory as the applet class
files.  When downloading from the net, the files to be read must be
served up by the same server that served the applet class files.

As for the MediaTracker, it is redundant in your code.  If you don't
need any information from the Image before displaying it, the
ImageObserver (the 'this' in the drawImage() method) will do everything
you need to get the image drawn.  Also the repaint() call is not
required either.

As Mark mentioned, you can embed the image file into a jar and serve
them up that way.

If you jar up your Applet, access the image file with;

getImage(getClass().getResource("fname.jpg"));

You can specify a directory for the image file but that directory must
be stored in your .jar file.

Signature

Knute Johnson
email s/nospam/linux/

Roedy Green - 06 May 2008 06:19 GMT
>That said, I have looked at Canadian Mind Products
> > P words > policy
[quoted text clipped - 4 lines]
>permission java.security.AllPermission;
>};

you have to correct the right file.  Your IDE may be using a different
JVM.

Find all the .policy files on your machine and fix them.

That's a fairly dangerous thing to do.  That turns off all security
for all apps anywhere.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

bH - 07 May 2008 05:14 GMT
On May 6, 1:19 am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:

> >That said, I have looked at Canadian Mind Products
> > > P words > policy
[quoted text clipped - 16 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

Hi Knute,
You wrote "If you jar up your Applet, access the
image file with; getImage(getClass().getResource
("fname.jpg"));

The applet is correctly jar'd along with the image
and shows as expected when the jar is opened.
I have tested to see that it does work.

I do not know what, if anything, goes into the ()
of getImage(getClass().getResource())
to exctract only the image from the jar by itself,
so that the image can go into
another program in the same folder.

import java.awt.*;
import java.applet.Applet;
import java.awt.Image;

public class ImageAppletBriefX extends Applet
 {
 private Image ioStream;
 private String errorMessage = null;
 public void init() {
   try
     {
     // the jar containing the image is
     //"TestLoadImage.jar"
     // the image file in the jar is "JBsm.JPG"
     ioStream = getImage(getClass().getResource());
     // << the line in question is above
     repaint();
     }
   catch (Exception netProblem )
     {
     errorMessage = "Could not reach image";
     }
   }

 public void paint( Graphics display)
   {
   if ( errorMessage == null )
     display.drawImage( ioStream, 0, 0, this );
   else
     display.drawString( errorMessage, 10, 10 );
   }
 }

Thanks for your help up to this point

bH

Hi Roedy,
I no longer desire to change the policy files.
your advice "That's a fairly dangerous thing to do.  That turns off
all security
for all apps anywhere" is well taken.

bH
Knute Johnson - 07 May 2008 06:41 GMT
> Hi Knute,
> You wrote "If you jar up your Applet, access the
[quoted text clipped - 47 lines]
>
> bH

I'm not exactly clear what your problem is here.  But I will give you a
complete example.  Assume you have an image file named "kittens.jpg" and
you want to display it in your Applet.  Furthermore you want to deploy
your Applet from a .jar file with the code and image store in the .jar.

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

public class test1 extends Applet {
    Image image;

    public void init() {
        image = getImage(getClass().getResource("kittens.jpg"));
    }

    public void paint(Graphics g) {
        g.setColor(Color.RED);

        if (!g.drawImage(image,0,0,this))
            g.drawString("Loading Image",10,20);
    }
}

Compile the code above.  Jar up the file with the following command;

jar cvfM test1.jar test1*.class kittens.jpg

This stores all class files from the test1 class and the image file into
the jar.

<html>
    <head>
    </head>
    <body>
        <applet archive="test1.jar" code="test1.class"
         width="640" height="480">
        </applet>
    </body>
</html>

Create the html file above, I called mine test1.html.

Now run the appletviewer or load the html file with your browser;

appletviewer test1.html

The appletviewer/browser will load the .jar file and run the
test1.class, reading the kittens.jpg image file from the .jar and
displaying it.  Until the image is completely loaded the message
"Loading Image" will be drawn onto the Applet as well.

You do not need a repaint() call if you are using the
ImageProducer/ImageObserver scheme.

If that is not what you needed, then please post again.

Signature

Knute Johnson
email s/nospam/linux/

bH - 07 May 2008 07:15 GMT
On May 7, 1:41 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > Hi Knute,
> > You wrote "If you jar up your Applet, access the
[quoted text clipped - 88 lines]
>      </body>
> </html>

> Create the html file above, I called mine test1.html.
>
[quoted text clipped - 23 lines]
>
> - Show quoted text -

Hi Knute,
I was able to accomplish your html model above earlier
and it did show the image as the original applet did,
Thank you for your html gift above.

Now for the rest of it. I was hoping that I might extract the
image "kittens.jpg" ONLY from the "test1.jar" and use the
the image in another applet. All be it by coding differently in a
second
applet, and make "kittens.jpg" appeared as coming from the jar.

I guess that what I was trying to do was make some sense out of
what Mark Space was saying above....
"Make this image into a resource, and use
getResourceAsStream().  That's the correct way to package extra files
with an applet."
Can this be changed to reflect getting the image from the jar?
image = getImage(getClass().getResource("kittens.jpg"));

Any possibility of coding to get just the image ("kittens.jpg") into
another applet using that with the idea that Mark Space is saying.
If that is not what he was suggesting then there is nothing more.

Thanks for your prompt reply.

bH
Knute Johnson - 07 May 2008 17:26 GMT
> On May 7, 1:41 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 129 lines]
> Can this be changed to reflect getting the image from the jar?
> image = getImage(getClass().getResource("kittens.jpg"));

getResourceAsStream() just gets you the image data in a stream as
opposed to the URL.  That won't help you with your question.

> Any possibility of coding to get just the image ("kittens.jpg") into
> another applet using that with the idea that Mark Space is saying.
[quoted text clipped - 3 lines]
>
> bH

Yes.  You can specify many .jar files in the ARCHIVE parameter of the
APPLET tag.  Just separate them with commas.

<html>
    <head>
    </head>
    <body>
        <applet archive="test1.jar,test2.jar" code="test2.class"
         width="640" height="480">
        </applet>
    </body>
</html>

The HTML above loads both the test1.jar and test2.jar and runs the class
test2.  The code for test2.java is almost the same as test1.java.

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

public class test2 extends Applet {
    Image image;

    public void init() {
        image = getImage(getClass().getResource("kittens.jpg"));
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLUE);

        if (!g.drawImage(image,0,0,this))
            g.drawString("Loading Image",10,20);
    }
}

Signature

Knute Johnson
email s/nospam/linux/

bH - 07 May 2008 20:52 GMT
On May 7, 12:26 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > On May 7, 1:41 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> > wrote:
[quoted text clipped - 187 lines]
>
> - Show quoted text -

Hi Knute,

Sorry but I cannot put this away because I cannot understand
something.
You have the image in a ("kittens.jpg") in a jar that has the name
"KnutesTest1.jar" (I made that name up here for the discussion).

I am now going to run your "test2" and I use your program above that
is supposed to access "KnutesTest1.jar" to grab the image
"kittens.jpg".

Yet in "test2" there is no clue that I can see where it is find the
image
you referred in test2:

"image = getImage(getClass().getResource("kittens.jpg"));"

It can't grab resources that, I don't think, that
are not stated.

Can you please clarify?

TIA,

bH
Knute Johnson - 07 May 2008 21:09 GMT
>> Yes.  You can specify many .jar files in the ARCHIVE parameter of the
>> APPLET tag.  Just separate them with commas.
[quoted text clipped - 68 lines]
>
> bH

The answer is in the HTML file.  Note that both test1.jar and test2.jar
are listed in the ARCHIVE element of the APPLET tag.  The browser or
appletviewer will load all .jar files listed there.  All classes and
files in both .jar files are available to the applet.  In the HTML above
I tell it to run the test2.class.  Does that explain it for you?

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

bH - 08 May 2008 03:12 GMT
On May 7, 4:09 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> >> Yes.  You can specify many .jar files in the ARCHIVE parameter of the
> >> APPLET tag.  Just separate them with commas.
[quoted text clipped - 85 lines]
>
> - Show quoted text -

Hi Knute,
Sorry to report that your latest gifts do not work: the image will not
be pulled out of
another jar.... plain and simple, not with the code that you wrote.

In summary the (test1.jar and test2.jar)  contain identical images and
making the test2.class to
look into test1.jar for the image, it shows nothing when the html page
shows the archive
to have both of the jars and even when I remove the second jar and
have only the
archive showing the first jar, and call for the test2.class to be
executed, no image
is displayed.

Also my research on the net shows nothing but confusion and
countless attempts to write something to get images out of the jars.

http://java.sun.com/docs/books/tutorial/deployment/jar/unpack.html
(comment: shows how to do it with the cmd.exe) my reaction "ugh"

http://forum.java.sun.com/thread.jspa?threadID=748643&messageID=4290284#4290284
(comment: Java Archive (JAR) Files - extracting to a certain
directory) instructions

http://forum.java.sun.com/thread.jspa?threadID=330269&messageID=1792680#1792680
(comment : topic  here is: I can't get some Images to load when they
are in jar files)

Knute,
Thanks again for your efforts.

I have used up my patience and possibly yours too.

bH
Knute Johnson - 08 May 2008 06:16 GMT
> On May 7, 4:09 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 101 lines]
>
> bH

This does work.  Go to http://rabbitbrush.frazmtn.com/test/ and click on
the test1.html file.  It will load two applets that both access the
images.jar file that contains my kittens.jpg image.  Look at the source
code for the HTML page and the two Java Applets, test1 and test2.  I put
these in a package but that is not required.  This is done every day
even though you've found a lot of people that are having problems, it
does work.

Signature

Knute Johnson
email s/nospam/linux/

bH - 08 May 2008 07:20 GMT
On May 8, 1:16 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > On May 7, 4:09 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> > wrote:
[quoted text clipped - 121 lines]
>
> - Show quoted text -

Hi Knute,

Thanks for the demo.

I didn't know that one could jar the image, never did that before.

I didn't see a manifest in your testX.jars, but who needs them when it
works like this.

I didn't see that way of writing the html page either.

As a mathematician who ordered a new suit from a tailor and at try on
time, said to his tailor "This suit doesn't fit. Look the right sleeve
of the jacket is too short.
And the jacket is too tight. Look at the pants, the left pants leg
needs to be shorter”.

To which the tailor replied "When you walk suck in your bay window,
hike up your right shoulder so that the right sleeve is at the same
length as the left, and take a longer step with your left leg. The
mathematician looked bewildered at the tailor's directions.

The tailor replied "You'll get used to walking that way, the way I had
to get used to figuring out math problems when I took the course you
taught".

bH
Lew - 08 May 2008 11:47 GMT
> As a mathematician who ordered a new suit from a tailor and at try on
> time, said to his tailor "This suit doesn't fit. Look the right sleeve
[quoted text clipped - 10 lines]
> to get used to figuring out math problems when I took the course you
> taught".

Having bought the suit, with his belly sucked in, one shoulder hiked, the hips
cocked so the pants legs would look the same length, torso twisted so the
jacket would drape, the mathematician left the tailor's shop, rather
disgruntled.  As he walked, hobbled, really, to keep the pants balanced, a man
walked up to him.

"Is that a new suit?  It looks nice!" the man said.

"Why, yes," the mathematician replied, thinking, maybe this tailor wasn't so
bad after all.

"Where'd you get it done?" the man inquired.

"At the tailor's on the corner back there," the mathematician responded.

"I've got to see him!" the man enthused.  "Anyone who can fit someone as
misshapen as you must be a genius!"

Signature

Lew

bH - 08 May 2008 12:29 GMT
> > As a mathematician who ordered a new suit from a tailor and at try on
> > time, said to his tailor "This suit doesn't fit. Look the right sleeve
[quoted text clipped - 31 lines]
> --
> Lew

Hi Lew,
Beautiful ending, I could not recall the "rest of the story", as it
was
nearly 1 A.M. in my part of the world when I wrote it, and especially
after frustrating days of digging the mines of Sun and Google,
looking for this diamond for the world to see.

bH


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.