Java GetResourceAsStream NullPointerException
我有一个jar,我通常在.sh脚本中使用jsvc启动器作为守护进程运行。 现在,我创建了一个新的入口点,它需要检查一些内容并在控制台上返回一个输出,因此在同一个.sh脚本中,我根据参数进行了切换,以作为守护进程或标准java应用程序运行。 sh与此类似:Java GetResourceAsStream NullPointerException,java,ubuntu,jar,classpath,jsvc,Java,Ubuntu,Jar,Classpath,Jsvc,我有一个jar,我通常在.sh脚本中使用jsvc启动器作为守护进程运行。 现在,我创建了一个新的入口点,它需要检查一些内容并在控制台上返回一个输出,因此在同一个.sh脚本中,我根据参数进行了切换,以作为守护进程或标准java应用程序运行。 sh与此类似: #!/bin/sh # Setup variables EXEC=/usr/bin/jsvc JAVA_HOME=/usr/lib/jvm/java-7-oracle CLASS_PATH="./conf:/usr/share/java/co
#!/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
我的core.jar只有在以“check”标志启动时才能读取我的core.properties文件,代码为:
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() {
...
}
}
我的文件夹结构如下:
root folder
|_ run.sh
|_ conf
|_ core.properties
|_ dist
|_ core.jar
|_ lib
|_ lib1.jar
|_ lib2.jar
|_ lib3.jar
|_ ....jar
|_ ....jar
我的jar清单如下所示:
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...
我看到包含了conf目录,但我不知道./conf/是否包含该目录中的所有文件。
我应该怎么做才能使属性文件作为守护进程和标准jar一样可读
谢谢我找到了解决办法。pom.xml中有一个问题,它给了类路径一个错误的前缀 这是:
<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>
maven jar插件
2.2
真的
.dist/lib/
xxxxxxx
/配置/
${project.artifactId}
${project.build.directory}/dist
与此相反:
<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>
maven jar插件
2.2
真的
/lib/
xxxxxxx
../conf/
${project.artifactId}
${project.build.directory}/dist
<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>