简体   繁体   English

无法在Hadoop jar上加载application.properties(NullPointerException)

[英]Unable to load application.properties on Hadoop jar (NullPointerException)

I've looked at all kinds of answers for this problem. 我已经看过这个问题的各种答案。 None of them work. 他们都没有工作。

I have the following code: 我有以下代码:

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ApplicationConfig {

    private static Logger LOG;

    private String appConfigFileLocation = "application.properties"; 
    private Properties appConfig;

    private static ApplicationConfig instance;

    public static ApplicationConfig getInstance() {
        if(instance == null) {
            instance = new ApplicationConfig();
        }

        return instance;
    } 

    private ApplicationConfig() {
        LOG = LoggerFactory.getLogger(this.getClass().getSimpleName());

        appConfig = new Properties();

        try {
            LOG.info("Reading config from " + appConfigFileLocation);

            appConfig.load(ClassLoader.getSystemResourceAsStream(appConfigFileLocation));

            LOG.info("Done reading config from " + appConfigFileLocation);
        } catch (FileNotFoundException e) {
            LOG.error("Encountered FileNotFoundException while reading configuration: " + e.getMessage());
            throw new RuntimeException(e);
        } catch (IOException e) {
            LOG.error("Encountered IOException while reading configuration: " + e.getMessage());
            throw new RuntimeException(e);
        }
    }
}

I created a JAR file. 我创建了一个JAR文件。 The JAR file has application.properties at the root. JAR文件在根目录下有application.properties I also copied the application.properties file in /etc/hadoop/conf and in the target/classes/ directory. 我还复制了/etc/hadoop/conftarget/classes/目录中的application.properties文件。

I use the hadoop jar command to execute the code. 我使用hadoop jar命令来执行代码。

But I keep getting the error: java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) 但是我一直收到错误: java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434)

Please help me at resolving this frustrating error! 请帮我解决这个令人沮丧的错误!

Found the error. 发现错误。

hadoop jar checks in the Hadoop classpath. hadoop jar检查Hadoop类路径。 Even though the file was there in the Hadoop classpath, it didn't have read permissions from the Hadoop user. 即使该文件存在于Hadoop类路径中,它也没有Hadoop用户的读取权限。

A simple sudo chmod a+r /etc/hadoop/conf/application.properties did the trick! 一个简单的sudo chmod a+r /etc/hadoop/conf/application.properties就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM