金陵之風塵

          灰塵,也有生活;
          它們在風中飄著,在煙中戀愛,在暖氣上撫摸;
          它們在好幾個地方找我……
          posts - 5, comments - 13, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          使用ANT,無法訪問conf目錄下的文件

          Posted on 2008-08-04 02:52 風塵仆仆 閱讀(608) 評論(1)  編輯  收藏 所屬分類: Java

               在使用ANT的過程中遇到了問題,工程目錄下的conf目錄存放的是一些配置文件,在程序中需要讀取這此文件。
              使用IDE(如JCreator)編譯運行程序,在IDE環境中增加了classpath路徑“E:\MyProject\conf”后,在Main.java中調用
                ClassLoader.getSystemResource("default.properties");
              可以查找到default.properties文件并獲得其路徑E:\MyProject\conf\default.properties。
              但是在使用ANT編譯運行時,正確設置了<classpath>,可還是無法調用ClassLoader.getSystemResource查找到文件。

          工程的目錄如下:
          E:\MyProject
           |- build
           |  |- classes
           |  |  |- app
           |  |     |- Main.class
           |  |- test
           |  |  |- app
           |        |- MainTest.class
           |
           |- src
           |  |- app
           |     |- Main.java
           |
           |- test
           |  |- app
           |     |- Main.Test.java
           |
           |- lib
           |  |- commons-lang-2.4.jar
           |
           |- conf
           |  |- default.properties
           |
           |- build.xml

          build.xml部分內容如下:
          <?xml version="1.0"?>

          <project name="myproject" default="test" basedir=".">
            <property name="build.dir" value="build"/>
            <property name="src" value="src"/>
            <property name="test.src" value="test"/>
            <property name="lib.dir" value="lib"/>
            <property name="conf.dir" value="conf"/>

            <path id="lib.path">
              <fileset dir="${lib.dir}">
                <include name="**/*.jar"/>
              </fileset>
            </path>
           
            <path id="run.classpath">
              <pathelement location="${build.dir}/classes"/>
              <pathelement location="${conf.dir}"/> <!-- 已經添加了conf目錄 -->
              <path refid="lib.path"/>
            </path>
           
            <!-- 測試default.properties是否存在 -->
            <available property="have.conf" resource="default.properties">
              <classpath refid="run.classpath"/>
            </available>
           
            <!-- 運行ant echo時,返回have.conf: true,證明可以找到文件 -->
            <target name="echo">
              <echo message="have.conf: ${have.conf}"/>
            </target>
           
            <!-- 運行ant run時,ClassLoader.getSystemResource("default.properties")返回卻為null -->
            <target name="run" depends="compile">
              <java classname="app.Main">
                <classpath refid="run.classpath"/>
              </java>
            </target>
          </project>


          評論

          # re: 使用ANT,無法訪問conf目錄下的文件  回復  更多評論   

          2008-08-07 23:51 by 風塵仆仆
          搞定了,不應該使用表態的ClassLoader.getSystemResource("default.properties");

          應該:
          ClassLoader classloader = Thread.currentThread().getContextClassLoader();
          URL url = classloader.getResource("default.properties");

          參考Log4j源代碼Loader.java

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 麻江县| 扎鲁特旗| 砚山县| 衡东县| 个旧市| 兴义市| 长治市| 慈利县| 西充县| 江川县| 金寨县| 东兰县| 墨竹工卡县| 龙陵县| 平昌县| 革吉县| 石首市| 桐柏县| 明溪县| 藁城市| 札达县| 武夷山市| 兴和县| 五家渠市| 桓仁| 德保县| 鄱阳县| 温泉县| 金堂县| 阿尔山市| 潮州市| 若羌县| 清丰县| 伊宁市| 南和县| 揭阳市| 深圳市| 榆林市| 师宗县| 绿春县| 桃源县|