我試圖構建一個簡單的應用程序。該應用程序包含一個可運行的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是一個好習慣?