0

What I have done so far is :

/** Default location of help files folder */
private static final String DEFAULT_PATH = "../../../../resources/files/help/";
/** A 404 error file */
private static final String ERROR_404 = "../../../../resources/files/help/404.html";

/**
 * @param fileName
 * @return The URL of file, if found at default location, otherwise a 404 error page URL.
 */
public static URL getURL(String fileName) throws MalformedURLException{
    URL url = (new File(ERROR_404)).toURI().toURL();
    System.out.println(url);
    url = (new File(DEFAULT_PATH + fileName)).toURI().toURL();
    System.out.println(url);
    return url;
}

Output:

file:/H:/My%20Project/Project%20Name%20Module/../../../../resources/files/help/404.html file:/H:/My%20Project/Project%20Name%20Module/../../../../resources/files/help/plazaCode.html

Folder Hierarchy in the JAR created through NetBeans:

enter image description here

I am on Windows 7, JDK 7.

UPDATE:

Actually I want this URL for a JTextPane to show a HTML page by method:

textPane.setPage(URL url);

Can I have any better solution than this? and with the same Folder Heirarchy.. ?

Asif
  • 4,980
  • 8
  • 38
  • 53

3 Answers3

1
  1. 404.html since this is an application resource, it will probably end up embedded in a Jar.
  2. Resources in archives cannot be accessed using a File object.
  3. URL getURL(String fileName) To help avoid confusion, change that to URL getURL(String resourceName).
  4. Use Class.getResource(String) much as discussed on your previous questions.
  5. 'Relative' URLs become dangerous by that stage, since they depend on the package of the class that calls them, I generally make them 'absolute' by prefixing the entire path with a single / which effectively means 'look for this resource, from the root of the run-time class-path'.

So that String might read (adjusting the rest of the code as already advised):

private static final String ERROR_404 = "/resources/files/help/404.html";
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

You can use URL url = getClass().getResource("..."). Probably "/files/help/404.html".

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

When you create a File in this way you will get a relative file (and therefore a relative URL). To get a absolute URL from that path you need to get an absolute File first. Try this:

new File(ERROR_404).getCanonicalFile().toURI().toURL()

(EDIT: After you JTextPane information above) I haven't ever tried this, so I have no clue on why that's not working. But its a different question, you should ask a new Question.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
  • Yeah, sorry. Took the wrong method. getCanonicalFile is the correct one, I'll edit my answer. – Angelo Fuchs Apr 06 '12 at 12:21
  • Again it did not work, Output: `file:/H:/resources/files/help/404.html file:/H:/resources/files/help/plazaCode.html`and same null pointer exception.. – Asif Apr 06 '12 at 12:26
  • Well, by Java Convention that is a valid and correct URL. Also, what NullPointerException? You didn't write about it in your question. (Be aware that file URLs as created from Java are not valid URLs outside of it, sometimes.) Please do provide me an example of what you would expect. – Angelo Fuchs Apr 06 '12 at 12:30
  • Sorry, the exception I got is from line `textPane.setPage(FileLoader.getURL())`, I just wanted to open a HTML file in the `textPane` with the folder heirarchy as i given through image – Asif Apr 06 '12 at 12:33
  • @Asif I'm sorry I haven't ever tried this. You should ask a new question. – Angelo Fuchs Apr 06 '12 at 12:38