编程语言
首页 > 编程语言> > 关于java打jar包后访问resource路径问题

关于java打jar包后访问resource路径问题

作者:互联网

在jar包中不能直接获取File文件,应当使用InputStream接收文件

原代码:

public static FileSystem getFileSystem(String hadoopConfPath) throws IOException {
        Configuration conf = new Configuration();

        File coreSiteFile= ResourceUtils.getFile("classpath:"+hadoopConfPath + "/core-site.xml");
        File hdfsFile = ResourceUtils.getFile("classpath:"+hadoopConfPath + "/hdfs-site.xml");

       if(!coreSiteFile.exists()){
            throw new RuntimeException("core-site.xml is not in " + hadoopConfPath);
        }
        if(!hdfsFile.exists()){
            throw new RuntimeException("core-site.xml is not in " + hadoopConfPath);
        }

        conf.addResource(corSiteFileStream);
        conf.addResource(hdfsFileStream);
        FileSystem fs = FileSystem.get(conf);

        return fs;
    }

打成jar包放入Linux,路径后报错,如下:

"class path resource [config/hadoop-conf.test/hadoop-conf/core-site.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/data/***/***.jar!/BOOT-INF/classes!/config/hadoop-conf.test/hadoop-conf/core-site.xml"

更正后:

 public static FileSystem getFileSystem(String hadoopConfPath) throws IOException {
        Configuration conf = new Configuration();

        InputStream corSiteFileStream = HDFSUtil.class.getClassLoader().getResourceAsStream(hadoopConfPath + "/core-site.xml");
        InputStream hdfsFileStream=HDFSUtil.class.getClassLoader().getResourceAsStream(hadoopConfPath + "/hdfs-site.xml");

        conf.addResource(corSiteFileStream);
        conf.addResource(hdfsFileStream);
        FileSystem fs = FileSystem.get(conf);

        return fs;
    }

标签:xml,core,包后,resource,hadoopConfPath,jar,site,conf,FileSystem
来源: https://www.cnblogs.com/ydjcom/p/14289212.html