java - Maven项目怎么读取resources配置文件?
伊谢尔伦
伊谢尔伦 2017-04-18 09:46:24
0
3
921

项目目录结构:

代码:

public class Generator {
    
    public void excute() {
        try {
            List<String> warnings = new ArrayList<String>();            
            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下了吗,为什么?该怎么改?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
洪涛
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("generatorConfig.xml");
巴扎黑

Download the maven projectresources目录下就是classpath.

巴扎黑

You are running the testgenerator under the test package, so filenotfound? If so, please create the java/test/resources directory. You can check your target directory. When running the test program, the configuration file is not generated to the compiled path. Then Naturally, the configuration file cannot be found

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template