This discussion is archived
9 Replies Latest reply: Oct 18, 2012 4:07 PM by EJP RSS

Applet: can not read file using getResource()

Hurukan Newbie
Currently Being Moderated
Greetings,

I'm working on an Applet that is about to read a pdf document.
When leaving the instruction
java.net.URL PDFUrl=CSSMBulletins.class.getResource("files/oldTransition.pdf");
PDFUrl is null...

This happens only when I test using a client browser (even if local or remote). When I debug using NetBeans this issue never happened...
The *.jar* file created (that I use in my Web Server's folder where the Index.html -- copy of launch.html -- is located) includes */icons* and */files* folders.
With objects in */icons* I never encountered any problems to get the URL, but with those in */files* I always have a null pointer returned by the method.
To be precise: the exception launched is of class nullpointerexception (the file oldTransition.pdf exists and is located in */files* folder

My question is:
How can I fix this issue ?

I added on both computers (local and remote) the java.io.FilePermission "/src/cssmbulletins/-","read,write"; statement in the virtual machine's java.policy file.

Edited by: Hurukan on Oct 17, 2012 6:18 PM

Edited by: Hurukan on Oct 17, 2012 6:19 PM

Edited by: Hurukan on Oct 17, 2012 6:20 PM

Edited by: Hurukan on Oct 17, 2012 6:39 PM

Edited by: Hurukan on Oct 17, 2012 6:41 PM
  • 1. Re: Applet: can not read file using getResource()
    DrClap Expert
    Currently Being Moderated
    "CSSMBulletins.class.getResource" is going to look for that resource in the same place that CSSMBulletins.class came from. Which, since it's in an applet, would be somewhere in the server where the applet was loaded from.

    However from your messing about with java.policy files it appears that you actually want to look in the client's file system from that PDF. Is that right? It isn't possible to give a solution to your problem until we know whether the file is supposed to come from the server or from the client.

    (And by running your server in Netbeans you obfuscated that requirement. That's why Netbeans testing is only a first step in the testing process.)
  • 2. Re: Applet: can not read file using getResource()
    Hurukan Newbie
    Currently Being Moderated
    Thank you DrClap...

    If I'm right the Applet is executed client side, by the client computer, but the jar file (which contains the /files /icons ressources) is located on the server.
    When I test the Applet with NetBeans, there is no problem (if I give the correct path to the method of course).
    But when testing from any browser (outside NetBeans) I got a null pointer for the PDFUrl.
    What that means ? Is there a "sandbox" issue ?
    The browser won't let no process read or write files ?
    That is the reason why I added some security policies in the clients's jvm as I did for the server for the tests with the browser in local (loopback). If there is nothing present in the java.policy file I can not connect to the database, cannot print and of course cannot read/write (when I did my tests with NetBeans I got security exception when trying to connect to the database, for example,...).
    I have not a deep knowledge of the java Applet environnement and I may have underestimated the complexity of "simply read/write a file" on the remote computer.
    I was thinking that if the virtual machine's policies were changed to accept the read or the write of a file it will run as expected.
    I was thinking that the .jar file (server side) was unpacked once the Applet launched in a "sandbox" controled by the client's virtual machine and everything that was within was hooked by the virtual machine (either it was a file or icons)... it seems it is more complicated... I assume that the Applet is unpacked in the local file system (even in memory).

    Do you think I have a good vision of how the Applet technology is working ? Or there is something important I missed ?
  • 3. Re: Applet: can not read file using getResource()
    EJP Guru
    Currently Being Moderated
    If I'm right the Applet is executed client side, by the client computer
    Correct.
    but the jar file (which contains the /files /icons ressources) is located on the server.
    But it is downloaded to the client.
    When I test the Applet with NetBeans, there is no problem (if I give the correct path to the method of course).
    But when testing from any browser (outside NetBeans) I got a null pointer for the PDFUrl.
    The basic question here, which you still haven't addressed, is whether the PDF inside the JAR file or somewhere else. getResource() will only load it from the JAR file if (a) that's where the class came from and (b) the PDF is in the JAR file. It's different in Netbeans because it is using build/classes instead of the JAR file as the CLASSPATH, so it can load directly from the disk.
    The browser won't let no process read or write files ?
    The Java Plugin won't let you do that unless you sign the applet or the .policy file permits it.
    That is the reason why I added some security policies in the clients's jvm as I did for the server for the tests with the browser in local (loopback).
    Don't do that. Deployment problems. Arguments with customers. Too hard. Sign the applet.
    If there is nothing present in the java.policy file I can not connect to the database, cannot print and of course cannot read/write (when I did my tests with NetBeans I got security exception when trying to connect to the database, for example,...).
    Sign the applet.
    I have not a deep knowledge of the java Applet environnement and I may have underestimated the complexity of "simply read/write a file" on the remote computer.
    So the PDF isn't in the JAR file? So it's expected to be in the client file system? In that case you can't use getResource() at all, you have to use File, FileInputStream, FileReader, etc.
    I assume that the Applet is unpacked in the local file system (even in memory).
    The JAR file(s) is/are unpacked in memory. If the PDF isn't in the JAR file it isn't unpacked.
    Do you think I have a good vision of how the Applet technology is working ? Or there is something important I missed ?
    (a) No (b) yes.
  • 4. Re: Applet: can not read file using getResource()
    Hurukan Newbie
    Currently Being Moderated
    I did a jar -tvf on the CSSMBulletins.jar... here is the selected results:
    303232 Wed Oct 17 18:33:40 CEST 2012 cssmbulletins/files/Professionnel.pdf
    306399 Wed Oct 17 18:33:40 CEST 2012 cssmbulletins/files/Technique.pdf
    252960 Wed Oct 17 21:30:26 CEST 2012 cssmbulletins/files/newTransition.pdf
    230602 Wed Oct 17 18:33:40 CEST 2012 cssmbulletins/files/oldTransition.pdf
    I dug to find out how to sign the Applet and I'm a little lost.
    NetBeans offers to sign the Applet automatically (self-signing), is that sufficient ?

    Is there a particular way to sign a .jar file ?
    Is that transparent to the end-user (because I had at least four messages asking me to trust the Applet or not but it hasn't solved the issue, with the self-signing feature at least) ?

    Thank you for the help in advance ^^

    I will try it again tomorrow... and go back if I can't sort me out ^^
  • 5. Re: Applet: can not read file using getResource()
    EJP Guru
    Currently Being Moderated
    I did a jar -tvf on the CSSMBulletins.jar... here is the selected results:
    What package is CMSBulletins.class in?
    I dug to find out how to sign the Applet and I'm a little lost.
    NetBeans offers to sign the Applet automatically (self-signing), is that sufficient ?
    It's a start. Ideally you should purchase a code signing certificate and sign with that. That gets rid of a browser dialog.
    Is there a particular way to sign a .jar file ?
    The jarsigner tool; the Ant jarsigner task; the Netscape thing you found; ...
    Is that transparent to the end-user (because I had at least four messages asking me to trust the Applet or not but it hasn't solved the issue, with the self-signing feature at least) ?
    That's because of the self-signed certificate. See above.
  • 6. Re: Applet: can not read file using getResource()
    DrClap Expert
    Currently Being Moderated
    This security business isn't obvious to a lot of people. The first hurdle to be got over is the idea that just because you claim to be trustworthy, that doesn't make you trustworthy and it doesn't mean that you should automatically be trusted by any software.

    If it isn't obvious why that is, consider that any scam artist or malware writer can claim to be trustworthy. How do I know that you aren't one when your applet arrives in my browser?

    And as long as you're using a self-signed certificate, that's exactly what's happening. You use the certificate by which you assert that you are trustworthy.

    So if you're serious about that applet, and you don't want to annoy your customers with having to decide whether you're a bad guy or not, you spend a few hundred bucks and get one of the commercial certificate-issuers to sell you a certificate. Once they decide you are trustworthy (and don't ask me how they decide that), they give you a certificate which will be automatically trusted by most browsers out there.
  • 7. Re: Applet: can not read file using getResource()
    EJP Guru
    Currently Being Moderated
    They don't decide you are trustworthy. They decide you are who you say you are. That establishes a legal chain of responsibility for the code back to you. I've never really understood why that entitles a browser not to ask the user whether the publisher is trusted. Maybe it does that, once; I'm always so glad to get rid of the dialogs that I don't take much notice of them.
  • 8. Re: Applet: can not read file using getResource()
    DrClap Expert
    Currently Being Moderated
    EJP wrote:
    I'm always so glad to get rid of the dialogs that I don't take much notice of them.
    Which is why you can nearly always get away with using a self-signed certificate. I've been party myself to installing software for commercial use which used a self-signed certificate and I never heard of any of our customers objecting.
  • 9. Re: Applet: can not read file using getResource()
    EJP Guru
    Currently Being Moderated
    I think it must only show the publisher dialog once for trusted certs, and every time for untrusted certs, but if anybody has any more reliable information I'd love to know.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points