0

sample.chm当用户单击帮助菜单项时,我正在尝试打开文件。我正在使用 NetBeans 7.01。我的类文件和帮助文件夹都在:

NetBeansProjects\MyApplication\src\org\me\myapplication

这是代码:

 private void helpContentsActionPerformed(java.awt.event.ActionEvent evt) {
 File f = new File("/help/sample.chm");
  try {
      Desktop.getDesktop().open(f);
  } catch (Exception ex) {
    System.out.println(ex);
  }
 }

错误:

java.lang.IllegalArgumentException: The file: \help\sample.chm doesn't exist.
4

2 回答 2

0

抛出异常是因为您提到的路径不正确。尝试给出绝对路径 File f=new File("/user/Documents/NetBeansProjects/MyApplication/src/help/sample.chm");

于 2012-03-03T09:43:20.023 回答
-1

如果您给出文件的绝对路径,它应该得到解决,如下所示:

import java.awt.Desktop;
import java.io.File;


public class FileOpen {
    private static void helpContentsActionPerformed(java.awt.event.ActionEvent evt) {
         File f = new File("C:/Users/compaq/Desktop/jsp.txt");// Absolute path of the file
          try {
              Desktop.getDesktop().open(f);
          } catch (Exception ex) {
            System.out.println(ex);
          }
         }
}
于 2012-03-03T10:48:15.343 回答