java - Maven项目怎么读取resources配置文件?

 mobiledu2502871343 发布于 2022-10-27 13:34

项目目录结构:

代码:

public class Generator {
    
    public void excute() {
        try {
            List warnings = new ArrayList();            
            ConfigurationParser cp = new ConfigurationParser(warnings);
            File  configFile = new File("." + File.separator + "generatorConfig.xml");
            Configuration config = cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(true);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            System.out.println("开始生成文件...");
            myBatisGenerator.generate(null);
            System.out.println("生成文件完成.");
        } catch (Exception e) {
            e.printStackTrace();
        }      
    }
  

}

项目是用来生成mybatis相关文件的,但是一直报错:java.io.FileNotFoundException: generatorConfig.xml (系统找不到指定的文件。)

编译时,resources中的文件不是已经在classes下了吗,为什么?该怎么改?

3 个回答
  • 你是运行测试包下的testgenerator 所以抱filenotfound吗 如果是 请建立java/test/resources目录 你可以看看你的target目录下应该在运行测试程序时 配置文件没有生成到编译后的路径下 那就自然找不到配置文件

    2022-10-28 11:17 回答
  • maven项目中resources目录下就是classpath下。

    2022-10-28 11:18 回答
  • ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream is = classloader.getResourceAsStream("generatorConfig.xml");
    2022-10-28 11:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有