public static  void  main(String[] args) throws Exception
   {
     IniReader aa =new IniReader("e:\\mail.properties");
    }

解决方案 »

  1.   

    这个是读配置文件的类,我在构造函数的地方 in = getClass().getResourceAsStream(strFileName);为空。
      

  2.   

    ....
    @see Class#getResourceAsStream(String)
    ..
    看看文档的描述,它并不是从绝对路径去搜索你的文件啊。。
    Finds a resource with a given name. 
    这个功能是由class loader来实现的,会委托到加载这个对象的class loader去实现。
    在委托之前,绝对资源名字会按照以下的算法从你给定的资源名称构造出来:
    (1) If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. 
    (2) Otherwise, the absolute name is of the following form: 
       modified_package_name/name
    所以,按照你的做法,实际去找的文件是classpath/e:/mail.properties,这个当然找不到了。针对你的问题,有两种修改的方法:
    (1) 既然你已经知道绝对路径了,这样做就可以了,没有必要用getResourceAsStream:
    <<
        in = new FileInputStream(new File(strFileName));
    >>(2) 把mail.properties放到classpath中
    <<
        IniReader aa = new IniReader("mail.properties");    在IniReader中:
        in = IniReader.class.getResourceAsStream("/"+strFileName);
    >>
    第二种做法会稍微合理一些。。
      

  3.   

    我这是在做测试的时候发现的问题。我最终的目的还是想把他写到相对路径中。如果用了您的第二中方法的话,以后给课户用都需要把mail.properties加到CLASSPATH中去吗?
      

  4.   

    我把mail.properties加进classpath中了,怎么还是不对呀?
      

  5.   

    是这样的,你程序不改,将e:加入classpath就可以了。如果觉得麻烦,可以将mail.properties放在与IniReader同一目录下,或者子目录中如:
    c:\IniReader.class
    c:\prop\mail.properties则
    可以这么写:
    IniReader aa =new IniReader("prop//mail.properties");
      

  6.   

    检查自己,俺做个小例子给你看看:
    程序:
    public class HelloWorld {
        public static void main(String[] args) throws Exception {
            java.io.InputStream is = HelloWorld.class.getResourceAsStream("/mail.properties");
            System.out.println(is);
        }
    }
    运行:
    $ java HelloWorld
    null
    假设把mail.properties放到conf目录下
    $ java -classpath .:conf HelloWorld
    java.io.BufferedInputStream@11a698a这样就可以了
      

  7.   

    哦是用JBUILDER做的,怎样把mail.properties放到classpath中呢?
    我用的是系统是2000,我直接在环境变量里设置也没用,用set classpath=...也没用。
      

  8.   

    应该是放到你所编译的类的class目录下