2011-05-16 53 views
2

我有一個Eclipse Web項目。我在根目錄下有一個名爲deploy.properties的文件,它似乎沒有被讀取。我有一種感覺,我可能需要將文件添加到「構建路徑」(如jar),但這只是一個猜測,當我嘗試這樣做時,沒有將文件添加到構建路徑的選項,以至於讓我覺得我錯了。Eclipse不會讀取配置文件

線89是這樣的一個props.load(stream);

我的堆棧跟蹤看起來是這樣的:

java.lang.NullPointerException 
at java.util.Properties$LineReader.readLine(Properties.java:365) 
at java.util.Properties.load(Properties.java:293) 
at sempedia.dao.Dao.getConfigFile(Dao.java:89) 
at sempedia.dao.Dao.<clinit>(Dao.java:17) 89 

和類看起來是這樣的:

public class Dao { 
private static final String configFileLocation = "/deploy.properties"; 

private static final Properties configFile = getConfigFile(configFileLocation); 

private static final String host = configFile.getProperty("mysql.host"); 
private static final String port = configFile.getProperty("mysql.port"); 
private static final String db = configFile.getProperty("mysql.db"); 
private static final String user = configFile.getProperty("mysql.user"); 
private static final String pwd = configFile.getProperty("mysql.pwd"); 

public static String getHost() { return host; } 

public static String getPort() { return port; } 

public static String getDb() { return db; } 

public static String getUser() { return user; } 

public static String getPwd() { return pwd; } 


public static Connection getCon() { 
    Connection con = null; 
    try { 
     String url = "jdbc:mysql://" + host + ":" + port + "/" + db; 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     con = DriverManager.getConnection(url, user, pwd); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } catch (InstantiationException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return con; 
} 

private static Properties getConfigFile(String fileName) { 
    Properties props = new Properties(); 
    try { 
     InputStream stream = Dao.class.getResourceAsStream(fileName); 
     props.load(stream); 
    } catch (IOException e) { 
     System.err.println("Error opening configuration file"); 
     System.exit(1); 
    } 

    return props; 
} 
} 
+0

令人毛骨悚然。看起來你正在做一些與我目前在工作中非常相似的東西。 :) – Neil 2011-05-16 07:49:36

+0

@尼爾...這是一個小世界:) – Ankur 2011-05-16 07:56:37

回答

2

請記住,當你閱讀文件在您的eclipse項目中,默認位置位於您的源目錄中(如果這是一個Web應用程序,稍後將轉換爲您的類目錄)。所以我的建議是嘗試將文件從項目根目錄移動到「src」目錄並重試。

+0

我試過了,可悲的是沒有運氣......它確實有意義,雖然 – Ankur 2011-05-16 07:56:59

+0

我不知道我第一次做錯了什麼,但我再次嘗試,它的工作....感謝Neil – Ankur 2011-05-16 07:58:09

+0

很高興聽到它。 :) – Neil 2011-05-16 08:02:48

0

方法如果目標文件不存在於類路徑中,則Dao.class.getResourceAsStream(fileName)將返回null作爲輸入流 - 這就是Nul​​lPointerException的原因。

您應該在調用props.load(stream)爲null之前捕獲它(現在只捕獲IOException)或測試輸入流;

你的意思是什麼根目錄?系統根目錄,應用程序源根目錄,應用程序工作目錄系統根目錄將是一個相當糟糕的地方放置你的配置文件。

使用「deploy.properties」(開始時不帶斜線)並將其放置到類路徑的根目錄(「classes」,「bin」或任何您稱之爲的目錄)中。

如果你把它放在你的源代碼目錄的默認套餐級別 - 無論是在源,或已添加的源目錄的目錄,它會被複制到類路徑中這樣進行編譯:

/app 
    /src 
    /config 
    deploy.properties 

現在將config目錄作爲源目錄添加到項目中。