简体   繁体   English

如何从jar打开html或chm文件?

[英]How to open html or chm file from jar?

I have "help.html" file in resource directory. 我在资源目录中有“ help.html”文件。 I can open it from Eclipse these ways: 我可以通过以下方式从Eclipse中打开它:

  1. As chm file: 作为chm文件:

    Runtime.getRuntime().exec("hh.exe res/help.html");

  2. As html file by default system browser: 默认情况下,系统浏览器为html文件:

     URL resource = getClass().getClassLoader().getResource("help.html"); File file = new File(resource.toURI()); Desktop.getDesktop().open(file); 

When I create executable jar file I cannot open "help.html" anymore. 创建可执行jar文件时,无法再打开“ help.html”。 Is there any way to open the file from jar archive? 有什么办法可以从jar存档中打开文件? I know I can save "help.html" outside jar and open it but this is not what I want. 我知道我可以将“ help.html”保存在jar外面并打开它,但这不是我想要的。 I want to have a single jar file. 我想要一个jar文件。

You can't do that. 你不能那样做。 This is beacause the system ( hh.exe or the default browser) can't open a file that is stored inside your JAR. 这是因为系统( hh.exe或默认浏览器)无法打开JAR内部存储的文件。

The best solution IMHO is to copy the resource to the system temporary folder, and then run either command to open it. 最好的解决方案恕我直言,是将资源复制到系统临时文件夹,然后运行任一命令将其打开。 This way, your file is still stored inside the JAR, but can be copied to a location outside of it just when it needs to be opened. 这样,您的文件仍存储在JAR内,但是仅在需要打开时才可以将其复制到文件之外的位置。

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

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