ant运行java程序时, 它不能加载指定properties文件, 怎么处理?

本文探讨了一段使用Ant构建工具的代码,在运行时无法正确加载配置文件的原因,重点关注类加载器的作用及路径配置对资源访问的影响。通过对比Eclipse运行环境,揭示了Ant脚本中`build.xml`配置与类路径设置的不当之处,并提供了解决方案。

 

运行时, 目录结构如下: 

 

    .

    |-- ./build

    |   `-- ./build/TestAntLoadFile.class

    |-- ./build.xml

    |-- ./dist

    |   |-- ./dist/icpFinder.jar

    |   `-- ./dist/icp-finder.properties

    |-- ./icp-finder_bak.properties

    `-- ./src

        `-- ./src/TestAntLoadFile.java

 

 可运如何代码时, 

 

 

 public class TestAntLoadFile {
    private static final String CUSTOMER_CONFIG_FILE_NAME = "icp-finder.properties";
    
    public static void main(String[] args) {
        InputStream custumerConfigIn = TestAntLoadFile.class.
                getClassLoader().getResourceAsStream(CUSTOMER_CONFIG_FILE_NAME);
        
        System.out.println("custumerConfigIn: " + custumerConfigIn);
    }

 }

 

build.xml中核心配置如下: 

 

	<path id="run.classpath">
	    <fileset dir = "${dist.dir}" >
   	        <include name="**/*.jar"/>
   	        <include name="**/*.properties"/>
   	        <include name="./icp-finder.properties"/>
	    </fileset>
	</path>

    <target name="run" depends="jar">
        <java fork="true" classname="TestAntLoadFile">
            <classpath>
                <path refid="run.classpath"/>
            </classpath>
            
        </java>     
    </target>
 

 

 

 

 

 显示结果:custumerConfigIn: null。 

 

 这是为什么? 我知道是ant脚本里写的可能有问题。 同样的代码在Eclipse中运行时显示是没问题的。 

 

实验代码见附件。 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值