2013-08-22 144 views
0

我有一個jar,我通常在我的.sh腳本中使用jsvc launcher作爲後臺進程運行。 現在我創建了一個新的入口點,需要檢查一些事情並在控制檯上返回一個輸出,所以在同一個.sh腳本中,我根據這些參數創建了一個開關,作爲守護程序或標準Java應用程序運行。 了SH與此類似:Java GetResourceAsStream空指針異常

#!/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添加的不能看我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() { 
     ... 
    } 
} 

唯一的例外是:

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) 

我的文件夾結構是這樣的:

root folder 
    |_ run.sh 
    |_ conf 
     |_ core.properties 
    |_ dist 
     |_ core.jar 
     |_ lib 
      |_ lib1.jar 
      |_ lib2.jar 
      |_ lib3.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來讀取?

謝謝

回答

0

我找到了解決方案。在pom.xml中有一個問題,它給classpath一個錯誤的前綴。

有這樣的:

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

,而不是這樣的:

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