代码之家  ›  专栏  ›  技术社区  ›  Paras

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

  •  0
  • Paras  · 技术社区  · 6 年前

    我看过这个问题的各种答案。它们都不起作用。

    我有以下代码:

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

    application.properties 归档 /etc/hadoop/conf target/classes/

    我使用 hadoop jar 命令来执行代码。

    java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434)

    0 回复  |  直到 6 年前
        1
  •  0
  •   Paras    6 年前

    找到了错误。

    hadoop jar 检查Hadoop类路径。即使文件在Hadoop类路径中,它也没有Hadoop用户的读取权限。

    sudo chmod a+r /etc/hadoop/conf/application.properties 我成功了!