我试图构建一个简单的应用程序。该应用程序包含一个可运行的jar和两个文件夹:lib和conf。我想最终得到这样的结构:蚂蚁。将属性文件添加到jar运行时类路径
/app
------/conf/main.props
------/lib/*.jar
------App.jar
Lib目录包含依赖关系jar和conf包含属性文件。我将两个dirs的内容添加到classpath中。依赖关系完美工作,但无法找到属性文件。这里是我的代码:
package com.example;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.Properties;
public class HelloAnt {
public static void main(String[] args) throws IOException {
BasicConfigurator.configure();
Logger logger = Logger.getLogger(HelloAnt.class);
logger.warn("Hello from ant!");
Properties props = new Properties();
props.load(ClassLoader.getSystemResourceAsStream("conf/main.props"));
logger.warn("Prop is: " + props.get("name"));
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://www.google.co.uk");
try {
HttpResponse response = client.execute(get);
String s = EntityUtils.toString(response.getEntity());
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
}
和Ant构建文件:
<project name="HelloWorld" basedir="." default="compile">
<property name="lib.dir" location="lib"/>
<property name="src.dir" location="src/main/java"/>
<property name="build.dir" location="build"/>
<property name="classes.dir" location="${build.dir}/classes"/>
<property name="app.dir" location="${build.dir}/hello-ant"/>
<property name="app.lib.dir" location="${app.dir}/lib"/>
<property name="app.conf.dir" location="${app.dir}/conf"/>
<property name="app.jar" location="${app.dir}/${ant.project.name}.jar"/>
<property name="file.jar" location="${app.dir}/${ant.project.name}.jar"/>
<property name="main.class" value="com.example.HelloAnt"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}"/>
</target>
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<fileset dir="src/main/resources">
<include name="*.props"/>
</fileset>
</path>
<target name="compile" depends="init">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="${app.dir}"/>
<mkdir dir="${app.lib.dir}"/>
<mkdir dir="${app.conf.dir}"/>
<copy todir="${app.conf.dir}">
<fileset dir="src/main/resources">
<include name="*.props"/>
</fileset>
</copy>
<echo message="compile.classpath"/>
<copy todir="${app.lib.dir}">
<fileset dir="${lib.dir}" includes="*.jar"/>
</copy>
<path id="runtime.classpath">
<fileset dir="${app.lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<pathconvert property="manifest.classpath" pathsep=" ">
<path refid="compile.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.props" to="conf/*.props"/>
</chainedmapper>
</mapper>
</pathconvert>
<jar destfile="${file.jar}" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
</project>
输出是:
在java.util中的异常线程 “main” 显示java.lang.NullPointerException 。属性$ LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.example.HelloAnt.main(未知来源) 0 [main] WARN com.example.HelloAnt - 你好,来自蚂蚁!
Nullpointer beacuse null null inputstream。那么我做错了什么?是否将外部的jar属性添加到classpath是一个好习惯?