首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.io.FileNotFoundException:/BOOT-inf/classes!/templates/

java.io.FileNotFoundException:/BOOT-inf/classes!/templates/
EN

Stack Overflow用户
提问于 2017-03-28 09:01:04
回答 3查看 6.3K关注 0票数 2

我正在使用springboot创建一个项目,它没有错误运行的想法,但是,运行app.jar文件,它会像下面这样运行异常

代码语言:javascript
运行
复制
java.io.FileNotFoundException: class path resource [templates/] cannot be resol
ed to absolute file path because it does not reside in the file system: jar:fil
:/E:/projects/help/target/zhx-help-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/templa
es/
    at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:21
)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(Ab
tractFileResolvingResource.java:52)
    at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.get
emplateLoaderForPath(FreeMarkerConfigurationFactory.java:338)
    at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.cre
teConfiguration(FreeMarkerConfigurationFactory.java:290)
    at org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
   afterPropertiesSet(FreeMarkerConfigurer.java:116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea
Factory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea
Factory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea
Factory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBea
Factory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getO
ject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegist
y.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetB
an(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBea
(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory

springboot版本:1.5.2

使用弹簧-数据-jpa

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-28 09:23:22

我看你用的是共济会。在春季引导中,您不能使用普通的File方法来获取模板,因为在运行可执行JAR时它们是不可访问的(当File在JAR中时不能作为资源加载)。

使用以下方法加载模板文件夹:

代码语言:javascript
运行
复制
cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), "templates"));

完整的例子:

代码语言:javascript
运行
复制
@Configuration
public class FreemarkerConfiguration {

    @Bean
    public freemarker.template.Configuration freemarkerConfig() throws IOException {
        freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
        cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader(), "templates"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        return cfg;
    }

}
票数 4
EN

Stack Overflow用户

发布于 2018-05-17 19:08:45

如果您使用的球衣与弹簧靴,泽西并不是很好的spring。造成此错误的原因是,jersey无法自动发现所有rest资源。要解决这个问题,请显式注册所有资源,注册包不起作用。希望这个问题将在未来版本的泽西解决。

公共类JerseyConfig扩展ResourceConfig {

代码语言:javascript
运行
复制
public JerseyConfig() {
    register(Resource1.class);
    register(Resource2.class);
    register(Resource3.class);
}

}

票数 4
EN

Stack Overflow用户

发布于 2017-03-29 11:59:30

FreeMarkerConfigurationFactory.class

代码语言:javascript
运行
复制
protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
    if(this.isPreferFileSystemAccess()) {
        try {
            Resource ex = this.getResourceLoader().getResource(templateLoaderPath);
            File file = ex.getFile();
            if(this.logger.isDebugEnabled()) {
                this.logger.debug("Template loader path [" + ex + "] resolved to file path [" + file.getAbsolutePath() + "]");
            }

            return new FileTemplateLoader(file);
        } catch (IOException var4) {
            if(this.logger.isDebugEnabled()) {
                this.logger.debug("Cannot resolve template loader path [" + templateLoaderPath + "] to [java.io.File]: using SpringTemplateLoader as fallback", var4);
            }

            return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath);
        }
    } else {
        this.logger.debug("File system access not preferred: using SpringTemplateLoader");
        return new SpringTemplateLoader(this.getResourceLoader(), templateLoaderPath);
    }
}

所以制作日志杠杆信息

代码语言:javascript
运行
复制
<logger name="org.springframework.web" level="INFO"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43065117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档