Java GetResourceAsStream NullPointerException

1k views Asked by At

I have a jar I usually run as daemon using jsvc launcher in my .sh script. Now I created a new entry point that needs to check some things and return an output on the console, so in the same .sh script I made a switch based on the parameters to run as daemon or as a standard java application. The sh is similar to this:

#!/bin/sh

# Setup variables
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-7-oracle
CLASS_PATH="./conf:/usr/share/java/commons-daemon.jar:./:./dist/core.jar"

do_exec()
{
    $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH $1 $CLASS
}

for i in `ls ./dist/lib/*.jar`
do
  CLASS_PATH=${CLASS_PATH}:${i}
done

case "$1" in
    start)
            do_exec
            ;;
    stop)
            do_exec "-stop"
            ;;
    check)
            java -jar ./dist/core.jar check
            ;;
    *)
            echo "usage: daemon {start|stop|restart|check}" >&2
            exit 3
            ;;
esac

My core.jar can't read my core.properties file ONLY if started with the "check" flag, and the code is:

public class Config {

    private final static String RESOURCE_NAME = "core.properties";
    private static Properties properties = null;

    public static synchronized void init() {
        if (properties != null) {
            return;
        }
        try {
            properties = new Properties();
            properties.load(Config.class.getClassLoader().getResourceAsStream(RESOURCE_NAME));  <--- problematic line
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

    public static String get() {
       ...
    }
}

The exception 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 me.core.util.Config.init(Config.java:28)
    at me.core.util.Config.get(Config.java:36)
    at me.core.Main.checkCoreStatus(Main.java:55)
    at me.core.Main.main(Main.java:33)

My folder structure is this:

root folder
    |_ run.sh
    |_ conf
         |_ core.properties
    |_ dist
         |_ core.jar
         |_ lib
             |_ lib1.jar
             |_ lib2.jar
             |_ lib3.jar
             |_ ....jar
             |_ ....jar

My jar manifest is as following:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Administrator
Build-Jdk: 1.7.0_21
Main-Class: me.zenfeed.Main
Class-Path: ./conf/ ./dist/lib/junit-4.11.jar ./dist/lib/hamcrest-core-1.3.jar ....etc...

I see that conf directory is included, but I don't know if ./conf/ include all of the files inside that directory. What should I do to make properties file readable as a daemon as well as standard jar?

Thank you

1

There are 1 answers

0
Andrea On

I found the solution. There was a problem in pom.xml, which gave the classpath a wrong prefix.

There was this:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <archive>
            <!-- Make an executable jar, adjust classpath entries-->
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>.dist/lib/</classpathPrefix>
                <mainClass>xxxxxxx</mainClass>
            </manifest>
            <!--Resources will be placed under conf/-->
            <manifestEntries>
                <Class-Path>./conf/</Class-Path>
            </manifestEntries>
        </archive>
        <finalName>${project.artifactId}</finalName>
        <outputDirectory>${project.build.directory}/dist</outputDirectory>
    </configuration>
</plugin>

instead of this:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <archive>
            <!-- Make an executable jar, adjust classpath entries-->
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>./lib/</classpathPrefix>
                <mainClass>xxxxxxx</mainClass>
            </manifest>
            <!--Resources will be placed under conf/-->
            <manifestEntries>
                <Class-Path>../conf/</Class-Path>
            </manifestEntries>
        </archive>
        <finalName>${project.artifactId}</finalName>
        <outputDirectory>${project.build.directory}/dist</outputDirectory>
    </configuration>
</plugin>