Regarding help file in JAVA

I am new to creating files in java. I created a help file "sample.chm" with a third-party tool, adding it to a java program with the package name as a "help" with the runtime class and built a jar. When I run the jar file, it gives me the error that "the file cannot be found, null pointer exception". I gave a relative path for identifying a file like "../help/sample.chm", but it does not work, and I tried with various classes to refine the path. But still the same mistake.

Ask you to help me fix it.

The jar can be placed on different systems and open this help file without any problems.

Hope my explanation is enough to identify the problem.

Hi,

Chandu

+3
source share
2 answers

If you have a file inside the jar, you cannot access it as usual. You can access it as follows:

URL helpFile=Thread.currentThread().getContextClassLoader().getResource("help/sample.chm");

The method used above ( getResource) will return a URL; if you want, you can get it like InputStreamusing getResourceAsStreaminstead.

+1
source

At least a workaround if a better solution does not appear. Use the method this.getClass().getClassLoader().getResourceto get the input stream to the help file inside the jar.

Copy the bytes to the new help file in the temp temp target folder and use this extracted file using an external help file viewer.

0
source

Source: https://habr.com/ru/post/1759267/


All Articles