[英]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.. ?
并具有相同的文件夹层次结构..?
404.html
since this is an application resource, it will probably end up embedded in a Jar. 404.html
因为这是一个应用程序资源,它最终可能会嵌入到 Jar 中。File
object.File
object 访问档案中的资源。URL getURL(String fileName)
To help avoid confusion, change that to URL getURL(String resourceName)
. URL getURL(String fileName)
为避免混淆,请将其更改为URL getURL(String resourceName)
。Class.getResource(String)
much as discussed on your previous questions.Class.getResource(String)
就像您之前的问题所讨论的那样。/
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):这样
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.