简体   繁体   English

如何从 jar 下的相对路径获取 URL?

[英]How can I get a URL from relative path under a jar?

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: 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文件:/H:/My%20Project/Project%20Name%20Module/../../../../resources/files/help/404.html 文件:/H:/My%20Project/Project%20Name%20Module /../../../../resources/files/help/plazaCode.html

Folder Hierarchy in the JAR created through NetBeans:通过 NetBeans 创建的 JAR 中的文件夹层次结构:

在此处输入图像描述

I am on Windows 7, JDK 7.我在 Windows 7,JDK 7。

UPDATE:更新:

Actually I want this URL for a JTextPane to show a HTML page by method:实际上,我希望JTextPane的这个URL通过方法显示 HTML 页面:

textPane.setPage(URL url);

Can I have any better solution than this?我能有比这更好的解决方案吗? and with the same Folder Heirarchy.. ?并具有相同的文件夹层次结构..?

  1. 404.html since this is an application resource, it will probably end up embedded in a Jar. 404.html因为这是一个应用程序资源,它最终可能会嵌入到 Jar 中。
  2. Resources in archives cannot be accessed using a File object.无法使用File object 访问档案中的资源。
  3. URL getURL(String fileName) To help avoid confusion, change that to URL getURL(String resourceName) . URL getURL(String fileName)为避免混淆,请将其更改为URL getURL(String resourceName)
  4. Use Class.getResource(String) much as discussed on your previous questions.使用Class.getResource(String)就像您之前的问题所讨论的那样。
  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'. “相对”URL 在那个阶段变得危险,因为它们依赖于调用它们的 class 的 package,我通常通过在整个路径前加上一个/来使它们成为“绝对”,这实际上意味着“从运行时类路径的根'。

So that String might read (adjusting the rest of the code as already advised):这样String可能会读取(如已建议的那样调整代码的 rest):

private static final String ERROR_404 = "/resources/files/help/404.html";

You can use URL url = getClass().getResource("...") .您可以使用URL url = getClass().getResource("...") Probably "/files/help/404.html".可能是“/files/help/404.html”。

When you create a File in this way you will get a relative file (and therefore a relative URL).当您以这种方式创建文件时,您将获得一个相对文件(因此也是一个相对 URL)。 To get a absolute URL from that path you need to get an absolute File first.要从该路径获取绝对 URL,您需要先获取绝对文件。 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. (编辑:在你上面的 JTextPane 信息之后)我从来没有尝试过这个,所以我不知道为什么它不起作用。 But its a different question, you should ask a new Question.但这是一个不同的问题,你应该问一个新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM