项目目录结构:
代码:
public class Generator { public void excute() { try { Listwarnings = 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下了吗,为什么?该怎么改?
你是运行测试包下的testgenerator 所以抱filenotfound吗 如果是 请建立java/test/resources目录 你可以看看你的target目录下应该在运行测试程序时 配置文件没有生成到编译后的路径下 那就自然找不到配置文件
maven项目中resources
目录下就是classpath
下。
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("generatorConfig.xml");