1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

loading image using getCodeBase()

Discussion in 'Software Development' started by Caliraj, Jan 31, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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.*;
    /*<applet code="applet4.class" width=400 height=400>
    </applet>*/
    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).........
     
  2. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650

    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 :

    <applet code="applet4.class" archive="applet4.jar" width="400" height="400">
    </applet>



    ;)
     
  3. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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.
     
  5. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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?
     
  6. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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.
     
  7. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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.....
     
  8. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Sorry,

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


    then

    jarsigner C:\nrk\myprograms\applets\applet4.jar yourName
     
  9. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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
     
  10. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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) :

    Code:
    [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
     
  11. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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 ...
     
  12. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    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
     
  13. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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)
     
  14. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    Make sure your file is really called Manifest.mf and not manifest.mf because jar won't detect it as Java is case sensitive.
     
  15. Caliraj

    Caliraj Thread Starter

    Joined:
    Mar 22, 2006
    Messages:
    145
    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?
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/539828

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice