I want to open help.chm file when click on the help button. At present , it is opened from the desktop when click help button. I copied and paste the file in one of the packages in my project. Is there a way to open this file from this package?
This is what I did to open the file from desktop
private void helpActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Runtime.getRuntime().exec("hh.exe C:/Users/toshiba/Desktop/help2.chm");
}
catch (Exception ex)
{
ex.printStackTrace();
System.out.println(ex.getMessage());
}
}
Use getResourceAsStream(String name) or getResources(String name) and provide the package name and resource name separated and prefixed by '/'.
For example if the resource is in the package Test:
/Test/help.chm:
Inside the class which you want to access the resource from you'd do (getResourceAsStream(String name)):
InputStream is=getClass().getResourceAsStream("/Test/help.chm");
or using getResources(String name):
URL file=getClass().getResource("/Test/help.chm");
for further help have a look at this great tutorial.
if your file is inside jar package you could get url in this way:
URL resource = getClass().getResource("yourFile.chm");
System.out.println("URL to resource: " + resource );
or you can try something like:
//it return the relative path
ResourcesLoader.class.getClassLoader().getResource("package1/resources/repository/yourFile.chm").toString(); // if file is inside package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With