3

我有一个在 Tomcat 6 上运行的 webapp,一个类检查目录是否存在,如果不存在则创建它 -

// make sure path exists
File f = new File(value);
if (!f.exists()) {
    if (!f.mkdirs()) {
        throw new RuntimeException("Failed creating directory " + value);
    }
}

现在,在我的本地机器上运行时一切正常,但在 EC2 实例上执行时,我在 catalina.out 看到以下内容 -

2012-05-30 06:57:28 main ConfigService [INFO] Directory path not exists, creating /usr/share/tomcat6/webapps/ROOT/WEB-INF/data/temp/currency
2012-05-30 06:57:28 main DefaultListableBeanFactory [INFO] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b4c1d7: defining beans [fetchAppRankingJob,fetchCurrencyRatesJob,fetchReviewsJob,sendDailyReportJob,importSalesReportJob,fetchSalesReportJob,processSalesReportsJob,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.scheduling.support.ScheduledMethodRunnable#0,org.springframework.scheduling.support.ScheduledMethodRunnable#1,org.springframework.scheduling.support.ScheduledMethodRunnable#2,org.springframework.scheduling.support.ScheduledMethodRunnable#3,org.springframework.scheduling.support.ScheduledMethodRunnable#4,org.springframework.scheduling.config.ScheduledTaskRegistrar#0,appsales-scheduler,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
2012-05-30 06:57:28 main ContextLoader [ERROR] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fetchCurrencyRatesJob' defined in file [/var/lib/tomcat6/webapps/ROOT/WEB-INF/classes/com/appround/collect/currency/FetchCurrencyRatesJob.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.appround.collect.currency.FetchCurrencyRatesJob]: Constructor threw exception; nested exception is java.lang.RuntimeException: Failed creating directory /usr/share/tomcat6/webapps/ROOT/WEB-INF/data/temp/currency
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
        at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.appround.collect.currency.FetchCurrencyRatesJob]: Constructor threw exception; nested exception is java.lang.RuntimeException: Failed creating directory /usr/share/tomcat6/webapps/ROOT/WEB-INF/data/temp/currency
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
        ... 37 more
Caused by: java.lang.RuntimeException: Failed creating directory /usr/share/tomcat6/webapps/ROOT/WEB-INF/data/temp/currency
        at com.appround.collect.ConfigService.getPathProperty(ConfigService.java:109)
        at com.appround.collect.currency.FetchCurrencyRatesJob.<init>(FetchCurrencyRatesJob.java:18)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
        ... 39 more

我更喜欢写入 /home/ec2-user/collect/data,尝试写入我的 webapp 目录,因为它应该具有写入 WEB-INF 文件夹的权限。

我已经尝试在 /usr/share/tomcat6/conf/catalina.policy 授予文件权限写入/读取该文件夹 -

grant {
    permission java.io.FilePermission "/home/ec2-user/collect/data", "read, write";
    permission java.io.FilePermission "/home/ec2-user/collect/data/*", "read, write";
}

尽管如此,还是没有运气。

有什么线索吗?

谢谢!

4

1 回答 1

3

我假设您使用的是 Amazon Linux,因为您提到了ec2-user.

/usr/share/tomcat6/webapps/在您尝试创建新文件夹的父文件夹上设置了哪些权限?通常运行 Tomcat 的用户(也应该称为tomcat)没有写入权限,除非您使用chmod u+w.

此外,除非您授予用户对该目录的权限,否则tomcat不允许用户写入其他用户的主目录(在您的情况下)。/home/ec2-user/您的示例在那里有点令人困惑,因为您提到尝试写入webapps文件夹但ec2-user在策略文件中指定 ' 的主目录。

一般来说,如果你想创建临时数据文件,我建议使用File.createTempFile(..)来创建它们,它们会自动放在标准的临时文件夹中。如果您想自己为数据文件创建一个专用文件夹,/var/tmp/可能更适合,因为它是临时文件的标准 unix 位置(/tmp/可能会在重新启动时被清除,因此请不要将任何持久文件放在那里)。

于 2012-05-30T09:30:09.637 回答