Eclipse 不会读取配置文件

发布于 2024-11-07 23:01:40 字数 2220 浏览 4 评论 0原文

我有一个 Eclipse Web 项目。我的根目录中有一个名为deploy.properties 的文件,但似乎无法读取。我有一种感觉,我可能需要将文件添加到“构建路径”(如 jar),但这只是一个猜测,当我尝试这样做时,没有选项将文件添加到构建路径,以便让我觉得我的想法是错误的。

第 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;
}
}

I have an Eclipse web project. I have a file called deploy.properties in the root directory which doesn't seem to get read. I have a feeling that I may need to add the file to the "build path" (like a jar), but this is just a guess, when I try to do this there is no option for adding files to the build path so that makes me think I am wrong about that.

Line 89 is this one props.load(stream);

My stack trace looks like this:

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 

And the class looks like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-11-14 23:01:40

请记住,当您读取 Eclipse 项目中的文件时,默认位置位于源目录中(如果这是一个 Web 应用程序,则稍后会转换为您的类目录)。所以我的建议是尝试将文件从项目根目录移动到“src”目录并重试。

Keep in mind that when you read files in your eclipse project, the default location is in your source directory (if this is a web application, that translates to your classes directory later). So my advice is to try moving the file from the project root directory to your "src" directory and retrying.

情深缘浅 2024-11-14 23:01:40

如果类路径中不存在目标文件,方法 Dao.class.getResourceAsStream(fileName) 将返回 null 作为输入流 - 这就是 NullPointerException 的原因。

您应该捕获它(现在您只捕获 IOException)或在调用 props.load(stream) 之前测试输入流是否为 null;

现在你的意思是根目录是什么?系统根目录、应用程序源根目录、应用程序工作目录?系统根目录是放置配置文件的一个相当糟糕的地方。

使用“deploy.properties”(开头不带斜杠)对其进行寻址,并将其放置在类路径的根目录中(“classes”、“bin” - 或任何您所说的名称)。

如果将其放置在源目录的默认包级别中 - 无论是在源中,还是在您添加为源目录的目录中,它将在编译期间复制到类路径,如下所示:

/app
  /src
  /config
    deploy.properties 

现在将 config 目录添加到项目作为源目录。

Method Dao.class.getResourceAsStream(fileName) returns null as the inputstream if the target file doesn't exist in the classpath - that's why the NullPointerException.

You should either catch it (now you only catch IOException) or test input stream before calling props.load(stream) for null;

Now what root directory do you mean? System root, app source root, app working directory? System root would be a rather bad place to put your config files.

Address it with "deploy.properties" (without the slash at the beginning) and place it in the root of your classpath ("classes", "bin" - or whatever you call it).

If you place it in the default package level of your source directory - either in the source, or a directory that you have added as a source directory, it will be copied to the classpath during compile like this:

/app
  /src
  /config
    deploy.properties 

and now add config directory to the project as a source directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文