如何获取Grails/Tomcat Webapp的当前工作目录?

5
我为Tomcat编写了一个Grails Web应用程序,它打开并读取./data/transactions/foobar上的Berkeley DB。
这段代码在我的Eclipse环境中运行良好。
但是当我将其移动到服务器时,它会抛出此异常。我不确定在将Grails部署为WAR文件作为Web应用程序时,服务器上的当前工作目录在哪里。
com.sleepycat.je.EnvironmentNotFoundException: (JE 4.1.10) ./data/transactions/Foobar
Home directory: ./data/transactions/Foobar ENV_NOT_FOUND: 
EnvironmentConfig.setAllowCreate is false so environment creation is not permitted, but there are no log files in the environment directory. Environment is invalid and must be closed.

我的Linux服务器上Tomcat的安装目录是/usr/share/tomcat6。Web应用的根目录是/usr/share/tomcat6/webapps。WAR目录安装在名为“bridge”的子目录下。我的Grails应用的WAR路径位于/usr/share/tomcat6/webapps/bridge中。
我尝试将./data/transaction/Foobar放入/usr/share/tomcat6/webapps和/usr/share/tomcat6/webapps/bridge中,但没有成功。
我也尝试将./data/transaction/Foobar放入/usr/share/tomcat6安装基础目录中,同样没有成功。
以下是Tomcat服务器显示的错误信息:
Error 500: Executing action [index] of controller[com.ecmhp.ecmdatabridge.generators.GenerateEmiDigestsController] 
caused exception: (JE 4.1.10) ./data/transactions/Foobar 
    Home directory: ./data/transactions/Foobar ENV_NOT_FOUND: 
    EnvironmentConfig.setAllowCreate is false so environment creation is not permitted, but there are no log files in the environment directory. Environment is invalid and must be closed.

Servlet:Grails

URI:/bridge/grails/generateEmiDigests/index.dispatch

意思是“使用Grails作为Servlet并通过URI /bridge/grails/generateEmiDigests/index.dispatch访问”。
Exception Message: (JE 4.1.10) ./data/transactions/Foobar 
Home directory: ./data/transactions/Foobar ENV_NOT_FOUND: 
EnvironmentConfig.setAllowCreate is false so environment creation is not permitted, but there are no log files in the environment directory. Environment is invalid and must be closed. 

Caused by: (JE 4.1.10) ./data/transactions/Foobar 
    Home directory: ./data/transactions/Foobar ENV_NOT_FOUND: 
    EnvironmentConfig.setAllowCreate is false so environment creation is not permitted, but there are no log files in the environment directory. Environment is invalid and must be closed.

有什么办法可以设置Tomcat WAR的当前工作目录,以便它能够找到Berkeley DB ./data/transaction文件?还有什么办法可以找出我的Grails应用程序启动时当前的工作目录?

2个回答

8
如果您可以获取“grailsApplication” bean,它可以自动注入到Grails框架的不同部分中,您可以尝试以下操作:
grailsApplication.mainContext.servletContext.getRealPath('/data/transactions/foobar')

1
如果您安装了Grails的控制台插件,您只需运行以下命令即可:

new File( 'test.html' ).absolutePath

对于我们的应用程序,这将返回/data/opt/tomcat/5.5/,因此我想您需要将您的目录放在usr/share/tomcat6/data/transactions内。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接