Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applet's getCodeBase returns null

Tags:

java

file

applet

I have an applet, and need to open a stream to a file. This file is a local file located where the applet and HTML file are:

    URL localURL = new URL(getCodeBase(), "pixs/icons.zip");
    InputStream localInputStream =localURL.openStream();

It used to work fine, but after upgrading to java 1.7 build 25, getCodeBase() always return null. This is actually documented!, Alas - there is no recommendation how to overcome it.

One things that had worked is to use full path:

   URL localURL = new URL("file:c:/myFolder/pixs/icons.zip");

Is there another option to resolve that without using full path?

like image 407
DuduArbel Avatar asked Apr 18 '26 12:04

DuduArbel


1 Answers

Perhaps you can use getDocumentBase instead. Depends on the structure of your setup, whether there is a close relation between the code base and the documents. No permanent solution: getDocumentBase was modified in the same way in 7u40, according to bug #8019177.

If not, then you can try using getResource to obtain a URL from your JAR, e.g. the class file of the applet, and then disassemble that URL to get at the location of the JAR and hence the code base. This is untested, so feel free to edit this post if you tried this.

Last but not least, since that change only affects local applets, you could run a (local or public) web server to serve that applet.

If you want a more official statement on this, I'll quote the #8017250 bug report:

If applet need to load resource:

  • if the resource is in applet JAR(s), they should be able to load it with ClassLoader.getResoruceAsStream directly, without needing the codebase information.
  • if the resource is in arbitary location, not inside applet JAR, they should have other ways to get to that location, since it's not part of the applet resource anyway. (e.g. user.home java system property, provided that their applet has all-permissions)

http://www.duckware.com/tech/java-security-clusterfuck.html (thanks to this post) mentions some other alternatives. The least likely one to be affected by future Oracle modifications apprers to be the use of location.href in the containing HTML page, e.g. writing the <applet> tag from JavaScript.

like image 171
MvG Avatar answered Apr 21 '26 00:04

MvG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!