2
votes

I'm trying to build a simple app. The App consists of a runnable jar and two folders: lib and conf. I want finally to get a structure like this:

/app  
------/conf/main.props  
------/lib/*.jar  
------App.jar  

The Lib dir contains dependencies jars and the conf contains properties file. I add content of both dirs to classpath. Dependencies work perfectly, but properties file couldn't be found. Here is my code:

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();
    }
 }
}

And ant build file:

<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>

Output is:

Exception in thread "main" java.lang.NullPointerException at java.util.Properties$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(Unknown Source) 0 [main] WARN com.example.HelloAnt - Hello from ant!

Nullpointer beacuse of null inputstream. So what am I doing wrong? And is it a good practice to add external to jar properties to classpath?

1

1 Answers

2
votes

This thread deals with a similar issue to yours. In a nutshell here's what you need to do to fix it:

In the Class-Path header in your MANIFEST.MF file you need to specify conf/ instead of conf/main.props. For a quick fix just change your mapper to the following:

   <chainedmapper>
       <flattenmapper/>
       <globmapper from="*.props" to="conf/"/>
   </chainedmapper>

Then in your code you should load the properties file like this : HelloAnt.class.getResourceAsStream("/main.props").

I was able to get past the error you were seeing with this method.