示例项目的结构为:
.
|-- ./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>
项目在eclipse中运行良好,有人有什么建议吗?
不是在类路径中包含属性文件本身,而是需要包含它所在的目录,例如:
<path id="run.classpath">
<fileset dir="${dist.dir}" >
<include name="**/*.jar"/>
</fileset>
<dirset dir="${dist.dir}" />
<pathelement path="${dist.dir}" />
</path>