# loading image using getCodeBase()



## Caliraj (Mar 22, 2006)

Hi,
Im trying to load an image using the getCodeBase() function.Now, if the image is in the same location, there is no problem. The program is as follows:
import java.applet.*;
import java.awt.*;
/*
*/
public class applet4 extends Applet
{
Image img;
public void init()
{
img=getImage(getCodeBase(),"doggy1.gif");
}
public void paint(Graphics g)
{
g.drawImage(img,40,40,80,80,this);
showStatus("Location : "+getCodeBase());
}
}

Now here is the issue. The class file is in C:\nrk\myprograms\applets. Now I moved the image file(doggy1.gif) from C:\nrk\myprograms\applets to c:\nrk\images
I just made one change in the code as follows :
img=getImage(getCodeBase(),"../doggy1.gif");
But I get an exception : java.security.AccessControlException: access denied (java.io.FilePermission \C:\
nrk\myprograms\doggy1.gif read) at java.security.AccessControlContext.checkPermission(AccessControlContext .java:264).........


----------



## Chicon (Jul 29, 2004)

Hi Caliraj,

It's normal.
For security reasons, applets are desgined to run from within an Internet browser cache and are only allowed to open files stored in the cache.

Fortunately, it is possible for an applet to open a file stored outside its required environment : to do so, the applet must be *signed*.

Pratically, it works like this :
when the user browses an HTML page with a signed applet, a certificate is proposed to him.
If the user accepts the certificate, then the applet will execute normally.


How to sign an applet ?
---------------------
1° You have first to create a certificate. There are specific tools in the JDK to build certificates.
In a DOS window, browse the directory of your Java compiler *javac.exe*, you'll notice the presence of the following files :
*keytool.exe, jar.exe, jarsigner.exe*

With the following instructions, you will create your own certificate :

*keytool -genkey -alias caliraj
keytool -export -alias caliraj -file caliraj.crt 
*

2° Your certificate is created. Now, you must transform your applet in a JAR file :

* javac C:\nrk\myprograms\applets\applet4.java
jar cvf applet4.jar applet4.class
*

3° Your JAR file is created. Last step : signature

*jarsigner applet4.jar caliraj*

In your HTML code, you've just to write this line :

*
*


----------



## Caliraj (Mar 22, 2006)

Hi Chicon,
Thanks for the reply.I would alos like to know what basically is the difference between getCodeBase() and getDocumentBase() i.e the circumstances in which the methods are used...
Could you help?
Thanks,
Cali


----------



## Chicon (Jul 29, 2004)

*getCodeBase()* returns the URL of the directory in which the applet is stored.

*getDocumentBase()* returns the URL of the document in which the applet is embedded.


----------



## Caliraj (Mar 22, 2006)

Hi chicon,
This is regarding the security and the certificate that you were talking about.When the image is also in the same directory as that of the class file, I do not get the security error.Only if I place the image file in another path, the error happens.Moreover, the image file was also a Read Only file.I had then removed the Read Only option.
Regarding the certificate, in the keytool statement, can I give any alias name or is it like I have to give the userid with which I login to my pc?


----------



## Chicon (Jul 29, 2004)

Caliraj said:


> Hi chicon,
> This is regarding the security and the certificate that you were talking about.When the image is also in the same directory as that of the class file, I do not get the security error.Only if I place the image file in another path, the error happens.Moreover, the image file was also a Read Only file.I had then removed the Read Only option.
> Regarding the certificate, in the keytool statement, can I give any alias name or is it like I have to give the userid with which I login to my pc?


Normal, when the applet and the file are stored in the same directory, you won't have any error.
The file could have stayed in read online if your applet is not designed to modify it.

BTW, you may give any alias name you want to the certificate.
When you're done creating your certificate (the .crt file), you may directly import it from your Internet Explorer for further tests :

- open Internet Explorer,
- click Tools,
- click Internet Options,
- select 'Content' tab then click the 'Certificates' button,
- a window will open : click the 'Import' button below the pane, a wizzard will open then you follow its intructions.


----------



## Caliraj (Mar 22, 2006)

Hi Chicon,
When I executed the "jar cvf applet4.jar applet4.class" statement, I get the following error:


C:\Program Files\Java\jdk1.5.0_09\bin>jar cvf applet4.jar applet4.class
applet4.class : no such file or directory
added manifest

So im not sure if Im going in the right way.....


----------



## Chicon (Jul 29, 2004)

Sorry,

*jar cvf C:\nrk\myprograms\applets\applet4.jar C:\nrk\myprograms\applets\applet4.class
*

then

*jarsigner C:\nrk\myprograms\applets\applet4.jar yourName*


----------



## Caliraj (Mar 22, 2006)

Hi Chicon,
When I executed "jarsigner C:\nrk\myprograms\applets\applet4.jar kailash ", I got the following error:
Failed to load Main-CLass manifestattribute from C:\nrk\myprograms\applets\applet4.jar


----------



## Chicon (Jul 29, 2004)

You may delete the JAR file you created as its manifest file seems to be incomplete.
You will rebuild it later.

1° You have to create a special file called the manifest file : it is needed sometimes to tell what libraries are needed or which class must be started in a first time.

Open Notepad and copy paste the 2 following lines in it (*beware in Java, ALL is case sensitive*) :


```
[SIZE=2]
Manifest-Version: 1.0
Main-Class: applet4
[/SIZE]
```
Save the file with the name *Manifest.mf* into in your *C:\nrk\myprograms\applets\* directory

2° Open your DOS window to rebuild your JAR file this way :

*jar cvmf C:\nrk\myprograms\applets\applet4.jar C:\nrk\myprograms\applets\Manifest.mf C:\nrk\myprograms\applets\applet4.class*

Then you may try,

*jarsigner C:\nrk\myprograms\applets\applet4.jar kailash*


----------



## Caliraj (Mar 22, 2006)

Hi CHicon,
I ahve done the following.....
jar cvmf C:\nrk\myprograms\applets\applet4.jar
Then I gave a space, typed "C:\nrk\myprograms\applets\Manifest.mf " followed by a space and then "C:\nrk\myprograms\applets\applet4.class"
I get the following error: Illegal option: C
Usage: jar {ctxu}[vfm0Mi] [jar-file] [manifest-file] [-C dir] files ...


----------



## Chicon (Jul 29, 2004)

Oops, I inverted the order of the parameters. It's cvfm and not cvmf ! Also, use c: instead of C:.

This way,

*jar cvfm c:\nrk\myprograms\applets\applet4.jar c:\nrk\myprograms\applets\Manifest.mf c:\nrk\myprograms\applets\applet4.class*


----------



## Caliraj (Mar 22, 2006)

Hi chicon,
Im getting a File not found Exception......I just typed as below...

jar cvfm c:\nrk\myprograms\applets\applet4.jar c:\nrk\myprograms\applets\Manifest.mf c:\nrk\myprograms\applets\applet4.class

Error:
java.io.FileNotFoundException: c:\nrk\myprograms\applets\Manifest.mf (The system
cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.tools.jar.Main.run(Main.java:122)
at sun.tools.jar.Main.main(Main.java:903)


----------



## Chicon (Jul 29, 2004)

Make sure your file is really called *Manifest.mf* and not *manifest.mf* because *jar* won't detect it as Java is case sensitive.


----------



## Caliraj (Mar 22, 2006)

Hi chicon,
I have named the file as Manifest.mf, in the location C:\nrk\myprograms\applets
Its contents are :
Manifest-Version: 1.0
Main-Class: applet4

Could you find any error in that?


----------



## Chicon (Jul 29, 2004)

There's no error. I've tested it on my computer and it works. 

In attachment, you will find a zip file that contents applet4.jar


----------

