2011-05-16 54 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目录作为源目录添加到项目中。