tags:

views:

52

answers:

2

Hi All,

I'm new to help file creation in java. I have created a help file "sample.chm" with a 3rd party tool, added it to a java program with package name as "help" calling with runtime class and build the jar. When I run the jar file it is giving me an error that the "file cannot be found, null pointer Exception". I have given a relative path to identify the file like "../help/sample.chm" still it is not working and I tried with various classes to ientify the path. But still the same error.

Request you to please help me in fixing it.

The jar can be placed in different systems and should open this help file with out any issues.

I hope my explanation is sufficient you to identify the problem.

Regards,

Chandu

+1  A: 

If you have a file inside a jar, you can't access it as you normally would. You can access it like this:

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 as an InputStream as well by using getResourceAsStream instead.

qmega
Instead of using `Thread.currentTread().getContexClassLoader()` you could just use `this.getClass().getClassLoader()`
Lee
Hi Lee, Thanks for the reply. I had used the above mentioned class but the problem is URL class refers to the helps file using absolute path starting from the drive name/folder name/.. etc. So when i run this in another system it will check the absolute path of help file as specified in the above format and this path would no longer exists in this system and because of that jar file is throwing exception that file not found or null pointer. So can you provide me another approach.
Chandu
A: 

At least a workaround unless a better solution pops up. Use the this.getClass().getClassLoader().getResource way to get an inputstream to the help file inside the jar.

Copy the bytes to a new help file in the target systems temp folder and use this extracted file with the external help file viewer.

Andreas_D