Link to home
Start Free TrialLog in
Avatar of ilya239
ilya239

asked on

opening a relative URI from a Java applet

How can a Java applet load a file from the same directory in which its .jar file resides, without knowing the absolute URL?  I.e. what string should I pass to javax.xml.parsers.DocumentBuilder.parse(), documented here:

http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.lang.String)

to tell it to load "myfile.svg" from the same directory from which the jar file was loaded?

In HTML I can use relative URLs, i.e. from "a.html" I can hyperlink to "b.html" without giving the full absolute URL, and the browser will know to use the "b.html" file in the same dir as the referring "a.html".   How do I get the equivalent of such relative addressing from inside a Java applet?

Thanks for help!
Avatar of malfunction84
malfunction84
Flag of United States of America image

Since the applet is running client-side, it doesn't know anything about where it's being hosted unless you tell it.  You're going to have to pass the base path to the applet as a <param>.
Avatar of ilya239
ilya239

ASKER

So, there is no such thing as a "relative URI" from within the applet?  In the HTML example, "a.html" does not know where it's loaded either, but the relative link in it to "b.html" is interpreted automatically to mean "the b.html in the same dir as the referring a.html".  There is nothing corresponding for applets?
ASKER CERTIFIED SOLUTION
Avatar of malfunction84
malfunction84
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ilya239

ASKER

Thanks!  What happens when the web page with the applet is loaded from the local file system (i.e. using the File | Open file... command in Mozilla)?   In that situation, is it possible for the applet to load a file from the same directory as the webpage and the applet?   I understand that in general, an applet needs to be signed to be able to access the local file system.  But for the special case when the applet is loading a file from the same dir from which both the applet and its html page got loaded, is it different?

Thanks for help!
I believe that will also work.  URLs can reference both local files ("file://...") and remote files ("http://...").
Avatar of ilya239

ASKER

Thanks a lot, that worked!